Record Grouping

Posted by John Tue, 01 Apr 2008 21:05:00 GMT

Example to go thru your User model and return all the signups by month / year, doing conditionals on them and grouping,

@users = User.find(
  :all, 
  :select => "MONTH(created_at) as month, YEAR(created_at) as year, COUNT(*) AS records",
  :conditions => ["admin = 0'"], 
  :group => "year, month")

You can use this in your view like,

<% for @user in @users %>
  <%=@user.month%>
  <%=@user.year%>
  <%=@user.records%> signups
<% end %>