Sorting forum topics by the most recent replies

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

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.