0

私が持っているとしましょう:

class ForumTopic < ActiveRecord::Base
  has_many :forum_posts
  named_scope :number_of_posts, ??????
end

class ForumPost < ActiveRecord::Base
  belongs_to :forum_topic
end

何を入れようかな????? 次のような検索ロジッククエリを許可します。

ForumTopic.descend_by_number_of_posts

どんな助けでも大歓迎です!

4

1 に答える 1

0

投稿数順に並べますよね?

:counter_cache次のように注文できるので、使用した方が簡単だと思います。

class ForumTopic < ActiveRecord::Base
  has_many :forum_posts
  named_scope :by_number_of_posts, :order => "forum_posts_count"
end

# controller
ForumTopic.by_number_of_posts.all

使用:counter_cacheするには、関連付けを変更する必要があります

class ForumPost < ActiveRecord::Base
  belongs_to :forum_topic, :counter_cache => true
end

forum_posts_countテーブルに列を作成しforum_topicsます。

これだと思います。

于 2010-05-11T19:42:23.383 に答える