In Rails 4, the way to do this is to create areas on your model.
class NewsFeed < ActiveRecord::Base scope :group_by_month, -> { group("date_trunc('month', created_at) ") } scope :exclude_user_ids, -> (ids) { where("user_id is not in (?)",ids) } end
And then you name it like this:
@counts = NewsFeed.exclude_user_ids(['1','2']).group_by_month.count
This will give you:
{2014-01-01 00:00:00 UTC=>313, 2014-02-01 00:00:00 UTC=>3131}
Then you output (haml):
- @counts.each do |m| = "Month: #{m[0].strftime("%b")}, Count: #{m[1]}"
This will lead to:
Month: Jan, Count: 313 Month: Feb, Count: 3131
source share