我有一个按年份分组的数组:
{
Tue,01 Jan 2019=>
[
#<BrokerStatistic
id: 78,user_id: 3,start_date: "2019-03-01",end_date: "2019-03-31",month_transactions_qty: 2,month_transactions_revenue: 0.108477e6,total_top_seller_trans_id: 7,total_top_seller_revenue_id: 7,total_top_seller_trans_qty: 2,total_top_seller_revenue_amount: 0.118738e6,plan_id: 1,listings_count: 12,total_transactions_qty: 3,total_transactions_revenue: 0.15033e6,created_at: "2019-11-09 20:17:27",updated_at: "2019-11-09 20:17:27",month_lease_transactions_qty: 1,month_lease_revenue: 0.70359e5>,#<BrokerStatistic
id: 76,start_date: "2019-05-01",end_date: "2019-05-31",month_transactions_qty: 1,month_transactions_revenue: 0.69548e5,total_top_seller_trans_id: 6,listings_count: 17,total_transactions_qty: 4,total_transactions_revenue: 0.219878e6,created_at: "2019-11-09 20:17:26",updated_at: "2019-11-09 20:17:26",month_lease_transactions_qty: 0,month_lease_revenue: 0.0>,#<BrokerStatistic
id: 73,start_date: "2019-07-01",end_date: "2019-07-31",month_transactions_revenue: 0.13548e5,total_top_seller_trans_qty: 3,listings_count: 18,total_transactions_qty: 5,total_transactions_revenue: 0.233426e6,#<BrokerStatistic
id: 77,start_date: "2019-08-01",end_date: "2019-08-31",month_transactions_qty: 3,month_transactions_revenue: 0.144394e6,total_top_seller_revenue_id: 6,total_top_seller_trans_qty: 6,total_top_seller_revenue_amount: 0.259082e6,total_transactions_qty: 8,total_transactions_revenue: 0.37782e6,#<BrokerStatistic
id: 75,start_date: "2019-09-01",end_date: "2019-09-30",month_transactions_revenue: 0.86409e5,total_top_seller_trans_qty: 7,total_top_seller_revenue_amount: 0.345491e6,total_transactions_qty: 9,total_transactions_revenue: 0.464229e6,month_lease_transactions_qty: 5,month_lease_revenue: 0.266134e6>,#<BrokerStatistic
id: 79,start_date: "2019-10-01",end_date: "2019-10-31",month_transactions_revenue: 0.33662e5,listings_count: 21,total_transactions_qty: 10,total_transactions_revenue: 0.497891e6,month_lease_revenue: 0.60502e5>,#<BrokerStatistic
id: 80,start_date: "2019-11-01",end_date: "2019-11-30",month_transactions_qty: 9,month_transactions_revenue: 0.644359e6,total_top_seller_trans_qty: 14,total_top_seller_revenue_amount: 0.929084e6,listings_count: 30,total_transactions_qty: 19,total_transactions_revenue: 0.114225e7,month_lease_transactions_qty: 3,month_lease_revenue: 0.250054e6>
],Mon,01 Jan 2018=>
[
#<BrokerStatistic id: 74,start_date: "2018-10-01",end_date: "2018-10-31",month_transactions_revenue: 0.41853e5,total_top_seller_trans_qty: 1,total_top_seller_revenue_amount: 0.41853e5,listings_count: 9,total_transactions_qty: 1,total_transactions_revenue: 0.41853e5,month_lease_revenue: 0.93477e5>
]
}
分组来自模型范围:
scope :statistics_grouped_by_year,-> (broker_id) do
where(user_id: broker_id).order(:start_date).group_by {|t| t.start_date.beginning_of_year}
end
在控制器内部。被这样称呼:
@statistics = BrokerStatistic.statistics_grouped_by_year(current_user.id).sort.reverse.to_h
每年,我需要总计:
month_transaction_qty,month_transactions_revenue:,month_lease_revenue,month_lease_transactions_qty
我要创建一个函数:
def get_broker_statistics_totals(statistics,year)
end
然后在视图中调用它并传递年份,但是我不确定如何遍历一组活动记录。