Wednesday, February 2, 2011

Month with number of count display for any model in your application

By using rails finder method, it is very easy to display the number of post and month accordingly.
Here is the query

>> Post.count(:order => 'DATE(created_at) DESC', :group => ["DATE_FORMAT(created_at,'%m/%Y')"])
=> #< OrderedHash {"06/2010"=>1, "03/2010"=>1, "02/2010"=>1}>

Result is a collection hash ordered in the form of latest month as first one, Collection contains the month as Key and number of count as Value.

If we run it through the each loop and it is very easy to display.

Thanks,
Vadivelan