Skip to content

Instantly share code, notes, and snippets.

@taylorbrooks
Created May 15, 2013 02:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taylorbrooks/5581330 to your computer and use it in GitHub Desktop.
Save taylorbrooks/5581330 to your computer and use it in GitHub Desktop.
def self.last_12_monthly
start_date = (Time.now - 11.months).beginning_of_month
end_date = Time.now.end_of_month
h = Donation.calculate(:sum, :gross_amount, conditions: {
created_at: (start_date..end_date) },
order: "date_trunc('month', created_at)",
group: ["date_trunc('month', created_at)"])
h = h.to_a.map {|k,v| [k.to_date, v]}
num_of_months = 0..11
m = Hash[(num_of_months.to_a.reverse).map { |month| [month.months.ago.beginning_of_month.to_date, 0]}].merge(Hash[h])
Hash[*m.to_a.flatten]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment