Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class InsuranceReportController < ApplicationController
- before_action :set_location
- def index
- assets = Asset.includes(:location).includes(:asset_class)
- assets = assets.where(location_id: @location.id) if @location
- sum_grouped_assets = assets.group(:asset_class_id).sum(:value)
- count_grouped_assets = assets.group(:asset_class_id).count(:id)
- @classes = []
- AssetClass.all.each do |klass|
- sum = sum_grouped_assets.find{|x| x[0] == klass.id}
- count = count_grouped_assets.find{|x| x[0] == klass.id}
- @classes << {
- name: klass.name,
- description: klass.description,
- total_count: count ? count[1] : 0,
- total_value: sum ? sum[1] : 0
- }
- end
- end
- private
- # Use callbacks to share common setup or constraints between actions.
- def set_location
- @location = Location.find(params[:id]) if params[:id]
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement