Topic モデルと Post モデルがあります。トピックには多くの投稿があり、投稿はトピックに属しています。
index
各トピックの最新の投稿に基づいてトピックのページを表示したい。最近の最後の投稿がトピック用であるほど、上部に表示されます。私は基本的に、通常のフォーラムが行うことをシミュレートしたいと考えています。トピックに返信すると、そのトピックがインデックス ページに上がります。
したがって、各トピックの最後の投稿の *created_at* に基づいてトピックを順序付ける必要があります。これはどのように行うことができますか?
topic_controller.rb
def index
@forum = Forum.find(params[:forum_id])
@topics = @forum.topics.find(:all, :order => "last_post_id DESC")
# last_post_id is a column of Topic that stores ID of the last post
end
post.rb
class Post < ActiveRecord::Base
belongs_to :topic
attr_accessible :content
end
トピック.rb
class Topic < ActiveRecord::Base
has_many :posts, :dependent => :destroy
belongs_to :forum
accepts_nested_attributes_for :posts, :allow_destroy => true
attr_accessible :name, :last_post_id, :posts_attributes
end