モデルの属性だけでなく、関連する子モデルの属性をソートできるスコープ メソッドを使用して、モデルでマルチレイヤー ソートを記述する方法を理解するのに問題がありますか?
より具体的に言えば、次のモデルがあり、それぞれが前のモデルの関連する子です (簡潔にするために、他のモデル メソッドと宣言を除外しました)。
class Course < ActiveRecord::Base
has_many :questions
# would like to write multi-layer sort here
end
class Question < ActiveRecord::Base
belongs_to :course, :counter_cache => true
has_many: :answers
end
class Answer < ActiveRecord::Base
belongs_to :question, :counter_cache => true
end
questions_count
最初に( my を介してcounter_cache
) 、次に、answer_count
最後に でコースを並べ替えたいのですがcreated_at
、すべてを 1 つのスコープ メソッドにまとめてCourse
モデルに入れる方法を考えていました。
ありがとう。