Skip to content

Instantly share code, notes, and snippets.

@chengjk84
Created August 4, 2009 13:20
range = "created_at #{(12.months.ago.to_date..Date.today).to_s(:db)}"
# .to_s(:db) is a helper to transform a date-range into SQL condition clause
# ex. above: => "created_at BETWEEN '2006-01-05' AND '2007-01-05'"
@users = User.count(:all, :conditions => range, :group => "DATE_FORMAT(created_at, '%Y-%m')", :order =>"created_at ASC")
# - count all users, whose created_at timestamp falls within the range specified above
# - group users by 'Year-Month', and count the number of occurences within each group
# ex. result:
# count | date
# ---------------
# 2 | 2005-10
# 24 | 2005-11
# 32 | 2005-12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment