Skip to content

Instantly share code, notes, and snippets.

@tiegz
Created April 7, 2009 16:05
Show Gist options
  • Save tiegz/91310 to your computer and use it in GitHub Desktop.
Save tiegz/91310 to your computer and use it in GitHub Desktop.
class Statistic < ActiveRecord::Base
named_scope :by_month_and_year, :select => "DATE_FORMAT(created_at, '%b %y')", :group => "DATE_FORMAT(created_at, '%b %y')", :order => :created_at
end
>> Statistic.by_month_and_year.size
=> 6 # SELECT count(DATE_FORMAT(created_at, '%b %y')) AS count_date_format_created_at_b_y FROM `statistics`
>> Statistic.by_month_and_year.all.size
=> 5 # SELECT DATE_FORMAT(created_at, '%b %y') FROM `statistics` GROUP BY DATE_FORMAT(created_at, '%b %y') ORDER BY created_at
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment