У меня есть массив, сгруппированный по годам:
{
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, user_id: 3, 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, total_top_seller_revenue_id: 7, total_top_seller_trans_qty: 2, total_top_seller_revenue_amount: 0.118738e6, plan_id: 1, 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, user_id: 3, start_date: "2019-07-01", end_date: "2019-07-31", month_transactions_qty: 1, month_transactions_revenue: 0.13548e5, total_top_seller_trans_id: 6, total_top_seller_revenue_id: 7, total_top_seller_trans_qty: 3, total_top_seller_revenue_amount: 0.118738e6, plan_id: 1, listings_count: 18, total_transactions_qty: 5, total_transactions_revenue: 0.233426e6, 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: 77, user_id: 3, start_date: "2019-08-01", end_date: "2019-08-31", month_transactions_qty: 3, month_transactions_revenue: 0.144394e6, total_top_seller_trans_id: 6, total_top_seller_revenue_id: 6, total_top_seller_trans_qty: 6, total_top_seller_revenue_amount: 0.259082e6, plan_id: 1, listings_count: 18, total_transactions_qty: 8, total_transactions_revenue: 0.37782e6, created_at: "2019-11-09 20:17:27", updated_at: "2019-11-09 20:17:27", month_lease_transactions_qty: 0, month_lease_revenue: 0.0>,
#<BrokerStatistic
id: 75, user_id: 3, start_date: "2019-09-01", end_date: "2019-09-30", month_transactions_qty: 1, month_transactions_revenue: 0.86409e5, total_top_seller_trans_id: 6, total_top_seller_revenue_id: 6, total_top_seller_trans_qty: 7, total_top_seller_revenue_amount: 0.345491e6, plan_id: 1, listings_count: 18, total_transactions_qty: 9, total_transactions_revenue: 0.464229e6, created_at: "2019-11-09 20:17:26", updated_at: "2019-11-09 20:17:26", month_lease_transactions_qty: 5, month_lease_revenue: 0.266134e6>,
#<BrokerStatistic
id: 79, user_id: 3, start_date: "2019-10-01", end_date: "2019-10-31", month_transactions_qty: 1, month_transactions_revenue: 0.33662e5, total_top_seller_trans_id: 6, total_top_seller_revenue_id: 6, total_top_seller_trans_qty: 7, total_top_seller_revenue_amount: 0.345491e6, plan_id: 1, listings_count: 21, total_transactions_qty: 10, total_transactions_revenue: 0.497891e6, 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.60502e5>,
#<BrokerStatistic
id: 80, user_id: 3, start_date: "2019-11-01", end_date: "2019-11-30", month_transactions_qty: 9, month_transactions_revenue: 0.644359e6, total_top_seller_trans_id: 6, total_top_seller_revenue_id: 6, total_top_seller_trans_qty: 14, total_top_seller_revenue_amount: 0.929084e6, plan_id: 1, listings_count: 30, total_transactions_qty: 19, total_transactions_revenue: 0.114225e7, created_at: "2019-11-09 20:17:27", updated_at: "2019-11-09 20:17:27", month_lease_transactions_qty: 3, month_lease_revenue: 0.250054e6>
], Mon, 01 Jan 2018=>
[
#<BrokerStatistic id: 74, user_id: 3, start_date: "2018-10-01", end_date: "2018-10-31", month_transactions_qty: 1, month_transactions_revenue: 0.41853e5, total_top_seller_trans_id: 7, total_top_seller_revenue_id: 7, total_top_seller_trans_qty: 1, total_top_seller_revenue_amount: 0.41853e5, plan_id: 1, listings_count: 9, total_transactions_qty: 1, total_transactions_revenue: 0.41853e5, created_at: "2019-11-09 20:17:26", updated_at: "2019-11-09 20:17:26", month_lease_transactions_qty: 1, 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
, а затем назовите это в представлении и укажите год, но я не совсем уверен, как перебрать сгруппированный массив активных записей.