Is there a way to use group by within find without doing find_by_sql? I am trying to group all post by date... showing the date and then all the posts by it... then the next date. Anyone have any code snippets for this? Thanks John Kopanas -- http://www.soen.info - where software engineering knowledge gets indexed http://cusec.soen.info - software engineering conference
Have you taken a look at the Calculations plugin? http://techno-weenie.net/blog/code/269/more-on-activerecord-calculations I haven''t used it yet, but it may help you out. Looks really interesting. --Ryan On 11/13/05, John Kopanas <john-O1KSuMybMhqBUy7/sJONFg@public.gmane.org> wrote:> Is there a way to use group by within find without doing find_by_sql? > > I am trying to group all post by date... showing the date and then > all the posts by it... then the next date. Anyone have any code > snippets for this? > > Thanks > > John Kopanas > > -- > http://www.soen.info - where software engineering knowledge gets indexed > http://cusec.soen.info - software engineering conference > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 11/13/05, Ryan Wood <ryan.wood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Have you taken a look at the Calculations plugin? > > http://techno-weenie.net/blog/code/269/more-on-activerecord-calculations > > I haven''t used it yet, but it may help you out. Looks really interesting.That probably won''t work. It''s mainly for aggregate queries. Another option is to group them by date yourself: @posts = Post.find :all @grouped_posts = @posts.inject({}) do |all_posts, post| (all_posts[post.created_at.to_date] ||= []) << post all_posts end Then in the view: # sorts chronologically <% @grouped_posts.keys.sort.each do |date| -%> <h2><%=h date.to_s(:long) %></h2> <ul> <% @grouped_posts[date].each do |post| -%> <li><%=h post.title %></li> <% end -%> </ul> <% end -%> -- rick http://techno-weenie.net
works great! :-) thanks On 14-Nov-05, at 12:12 AM, Rick Olson wrote:> On 11/13/05, Ryan Wood <ryan.wood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Have you taken a look at the Calculations plugin? >> >> http://techno-weenie.net/blog/code/269/more-on-activerecord- >> calculations >> >> I haven''t used it yet, but it may help you out. Looks really >> interesting. > > That probably won''t work. It''s mainly for aggregate queries. > > Another option is to group them by date yourself: > > @posts = Post.find :all > > @grouped_posts = @posts.inject({}) do |all_posts, post| > (all_posts[post.created_at.to_date] ||= []) << post > all_posts > end > > Then in the view: > > # sorts chronologically > <% @grouped_posts.keys.sort.each do |date| -%> > <h2><%=h date.to_s(:long) %></h2> > <ul> > <% @grouped_posts[date].each do |post| -%> > <li><%=h post.title %></li> > <% end -%> > </ul> > <% end -%> > -- > rick > http://techno-weenie.net > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails