andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jun-22 01:45 UTC
How to sort on a join... kindof.
So i''ve got two models: foo has_many bars bar belongs_to foo And then I can find the newest bar since I''m using nifty created_at columns... @foo=Foo.find(1) @foo.bar.last.whatever But lets say I have much foo @foos=Foo.find(:all) @foos.each do | f | f.bar.last.whatever end But what I really want to do is sort the foos in order of the last bar created for each foo. But I can''t figure out how to do it. The specifics are topics and posts, and of course I''m trying to do a paginating find... @topics_pages, @topics = paginate(:topics, :conditions => ["group_id = ?", @group.id], :order => "what in the world do i put here...") It''s almost like I have to do a join in sql here, but then I end up pulling in all the posts in to the array at the time of the find, when all I want to do is get the topic so I can later do a topic.post.last Any great ideas? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jun-22 14:51 UTC
Re: How to sort on a join... kindof.
OK, so I think I can just get the topics into @topics, and then sort the array in rails on topic.post.last, but I don''t know how to do it... am I going down the right path here? ---A On Jun 21, 9:45 pm, "andrew.ohns...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <andrew.ohns...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> So i''ve got two models: > > foo has_many bars > bar belongs_to foo > > And then I can find the newest bar since I''m using nifty created_at > columns... > > @foo=Foo.find(1) > @foo.bar.last.whatever > > But lets say I have much foo > > @foos=Foo.find(:all) > @foos.each do | f | > f.bar.last.whatever > end > > But what I really want to do is sort the foos in order of the last bar > created for each foo. But I can''t figure out how to do it. The > specifics are topics and posts, and of course I''m trying to do a > paginating find... > > @topics_pages, @topics = paginate(:topics, :conditions => > ["group_id = ?", @group.id], :order => "what in the world do i put > here...") > > It''s almost like I have to do a join in sql here, but then I end up > pulling in all the posts in to the array at the time of the find, when > all I want to do is get the topic so I can later do a topic.post.last > > Any great ideas?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
:order => "created_at" or :order => "created_at DESC" based on which way u want it sorted. If you wanted to sort a collection, you could also do: --------- for thing in @things.sort { b.created_at <=> a.created_at } #loop thru your things and display them end --------- or you could use sort_by (see link below). and for more on sorting: http://redcorundum.blogspot.com/2006/10/using-sortby-instead-of-sort.html -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Dave Coleman wrote:> --------- > for thing in @things.sort { b.created_at <=> a.created_at } > #loop thru your things and display them > end > ---------I probably should have read what I typed. it would be: --------- for thing in @things.sort {|a,b| b.created_at <=> a.created_at } #loop thru your things and display them end --------- -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Fri, 22 Jun 2007, Dave Coleman wrote:> > --------- > for thing in @things.sort {|a,b| b.created_at <=> a.created_at } > #loop thru your things and display them > end > ---------More idiomatically: @things.sort_by {|x| x.created_at}.each do |thing| #loop through your things and display them end Ought to be quicker, but will use more memory. ----------------------------------------------------- Enumerable#sort_by enum.sort_by {| obj | block } => array ------------------------------------------------------------------------ Sorts _enum_ using a set of keys generated by mapping the values in _enum_ through the given block. %w{ apple pear fig }.sort_by {|word| word.length} #=> ["fig", "pear", "apple"] [...] Hugh --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Jun-22 18:09 UTC
Re: How to sort on a join... kindof.
I''d rather do it in SQL, but I didn''t think :order => "created_at" would work... the data being pulled in is a list of topics. I do not want to sort on the "created_at" of the topic, but on the "created_at" of the last post associated with the topic. Topic 1 was created 2 days ago. It has 2 posts, the newest post is 4 hours old. Topic 2 was created 1 day ago. It has 2 posts, the newest post is 8 hours old. Topic 3 was created 1 hour ago. It has 2 posts, the newest post is 30 minutes old. The correct sort order should be 3, 1, 2. If I just do :order => "created_at" then the order will be 3, 2, 1. I am using the .last function to find the last post, but of course that only exists in rails. If I specify the post<->topic join in the .find then I can do :order => "posts.created_at" but the side effect of that is a huge array returned to rails with all the posts for a particular topic, when all I need is the created_at of the .last one. I think I''m looking at a sort in rails, after the list of topics has been pulled from the db. Rails'' .sort and sort_by do not make any sense to me yet, so I''ll have to do some reading and figure them out. :) ---A On Jun 22, 11:38 am, Dave Coleman <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> :order => "created_at" > or > :order => "created_at DESC" > > based on which way u want it sorted. > > If you wanted to sort a collection, you could also do: > > --------- > for thing in @things.sort { b.created_at <=> a.created_at } > #loop thru your things and display them > end > --------- > > or you could use sort_by (see link below). > > and for more on sorting:http://redcorundum.blogspot.com/2006/10/using-sortby-instead-of-sort.... > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---