I have an ItemsSold model which has the total number of magazines, books, videos, greeting_cards, pens sold for a single day. How do I elegantly return an array with the weekly sold totals for each item for the last arbitrary number of weeks?
I have a model file:
#items_sold.rb
class ItemsSold < ActiveRecord::Base
attr_accessible :magazines, :books, :videos, :greeting_cards, :pens, :sold_date
end
My table is defined as follows:
t.integer :magazines
t.integer :books
t.integer :videos
t.integer :greeting_cards
t.integer :pens
t.datetime :sold_date
Also, is it possible to return monthly totals for the last year, too?
See ActiveRecord::Calculations#sum
That will return a hash like
{1 => {:magazines => 5, :books => 33, ...}, 2 => {{:magazines => 13, :books => 28, ...}}, ...}
, where each key is a week number (1 for this past week, 2 for two weeks ago, etc..) and the value is another hash with the sums.