Let’s say I have:
class ForumTopic < ActiveRecord::Base
has_many :forum_posts
named_scope :number_of_posts, ??????
end
class ForumPost < ActiveRecord::Base
belongs_to :forum_topic
end
What should I put in ????? to allow searchlogic query like:
ForumTopic.descend_by_number_of_posts
Any help would be very greatly appreciated!
Do you want to order by number of posts, right?
I think it’s easier if you use
:counter_cachebecause if you do, you can order just like this:To use
:counter_cacheyou’ll need to change the associationand create a
forum_posts_countcolumn onforum_topicstable.I believe this is it.