Can''t quite figure this one, I have a School (has_many reviews) and a Review (belongs_to school). I''m rendering my reviews per school like this "@School.reviews" Which renders the "_review.html.erb" multiple times for reviews of that school (works a treat). But how do I control the order in which these are displayed (I''d like to reverse them basically most recent on top). bb. -- 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 -~----------~----~----~----~------~----~------~--~---
this should work: @school.reviews.sort_by { |cc| - cc.updated_at.to_i } --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
class School < AR has_many :reviews, :order => ''created_at DESC'' end class Review < AR belongs_to :school end If you use @school.reviews you''ll get the desired effect.... MaD escribió:> this should work: > @school.reviews.sort_by { |cc| - cc.updated_at.to_i } > > > >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That''s ideal! thanks. Follow up question, what if I want to override this order from time to time, say on some occasions have the reviews sorted by their last_name column? -- 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 -~----------~----~----~----~------~----~------~--~---
You could use named_scope to get more complexity. I think that''s a possibility. http://www.locomotivation.com/blog/2008/08/25/simplify-activerecord-aggregates-and-other-goodies-via-named-scope.html bingo bob escribió:> That''s ideal! thanks. > > Follow up question, what if I want to override this order from time to > time, say on some occasions have the reviews sorted by their last_name > column? >--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Bob, You could also render it with jquery or some javascript library that enables you to write the set of reviews once to javascript and present and re-present in multiple ways as needed without more roundtrips to the server. Al On Feb 9, 3:18 am, Juan José Vidal <jua...-eCpCCBycay/QT0dZR+AlfA@public.gmane.org> wrote:> You could use named_scope to get more complexity. I think that''s a > possibility. > > http://www.locomotivation.com/blog/2008/08/25/simplify-activerecord-a... > > bingo bob escribió: > > > That''s ideal! thanks. > > > Follow up question, what if I want to override this order from time to > > time, say on some occasions have the reviews sorted by their last_name > > column?--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---