I have three related models such as the following, each being the child of the one above:
class Course < ActiveRecord::Base
has_many :questions
end
class Question < ActiveRecord::Base
belongs_to :course
has_many: :answers
default_scope order: 'questions.created_at DESC'
scope :by_answer_count, -> { #orders Questions based on its answer count
joins(:answers).reorder("count(answers.id) DESC").group(:id)
}
end
class Answer < ActiveRecord::Base
belongs_to :question
end
What I am having trouble figuring out is: how do I use my scope method by_answer_count
, which is in my Question
model, to order the list of courses I show by the most number of answers to the least in the index
action of CoursesController
? Is there a way to utilize that, or should I write a 2 layers down scope method in my CoursesController
to get the filter to work?
Thanks!