I have built a simple forum but am having difficult getting the topics to display in the order of their latest reply. The associations are as follows: class Forum < ActiveRecord::Base has_many :topics, :order => "sticky DESC, created_at DESC" has_many :replies, :through => :topics class Topic < ActiveRecord::Base belongs_to :forum has_many :replies, :order => "created_at", :dependent => :destroy class Reply < ActiveRecord::Base belongs_to :topic And in my controller I am calling @topics = @forum.topics What I would like to do is sort the topics by the created_at of their most recent posts. Any advice appreciated. Thanks, Dan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Jack Christensen
2010-Mar-23 20:52 UTC
Re: Sorting forum topics by the most recent replies
On 3/23/2010 2:56 PM, DanC wrote:> I have built a simple forum but am having difficult getting the topics > to display in the order of their latest reply. > > The associations are as follows: > > class Forum< ActiveRecord::Base > has_many :topics, :order => "sticky DESC, created_at DESC" > has_many :replies, :through => :topics > > class Topic< ActiveRecord::Base > belongs_to :forum > has_many :replies, :order => "created_at", :dependent => :destroy > > class Reply< ActiveRecord::Base > belongs_to :topic > > And in my controller I am calling @topics = @forum.topics > > What I would like to do is sort the topics by the created_at of their > most recent posts. > > Any advice appreciated. >Add a last_reply_at field to Topic. Add a after_save to Reply that updates last_reply_at. When you find all topics sort by last_reply_at.> Thanks, > > Dan > >-- Jack Christensen jackc-/SOt/BrQZzMOf2zXYvRtkodd74u8MsAO@public.gmane.org -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.