0

假设我有:

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

class ForumPost < ActiveRecord::Base
  belongs_to :forum_topic
end

我应该放什么??????允许 searchlogic 查询,例如:

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_countforum_topics

我相信就是这样。

于 2010-05-11T19:42:23.383 回答