Ruby on rails 使用命名范围查找关联行的数量(RubyonRails+;Searchlogic问题)

Ruby on rails 使用命名范围查找关联行的数量(RubyonRails+;Searchlogic问题),ruby-on-rails,named-scope,searchlogic,Ruby On Rails,Named Scope,Searchlogic,假设我有: class ForumTopic < ActiveRecord::Base has_many :forum_posts named_scope :number_of_posts, ?????? end class ForumPost < ActiveRecord::Base belongs_to :forum_topic end 任何帮助都将不胜感激 你想按帖子数量排序,对吗 我认为如果您使用:counter\u cache会更容易,因为如果您这样做,您可

假设我有:

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

class ForumPost < ActiveRecord::Base
  belongs_to :forum_topic
end

任何帮助都将不胜感激

你想按帖子数量排序,对吗

我认为如果您使用
:counter\u 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
并在
论坛主题
表上创建一个
论坛帖子数

我相信就是这样

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
class ForumPost < ActiveRecord::Base
  belongs_to :forum_topic, :counter_cache => true
end