【发布时间】:2013-02-03 17:58:50
【问题描述】:
我无法弄清楚如何在我的模型中使用范围方法编写多层排序,该方法可以对模型的属性及其相关子模型的属性进行排序?
更具体地说,我有以下模型,每个模型都是前一个模型的相关子代(为简洁起见,我排除了其他模型方法和声明):
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(通过我的counter_cache)对课程进行排序,然后按answer_count,最后按created_at,我想知道如何将所有内容都串起来一起放入单个范围方法中,以放入我的 Course 模型中。
谢谢。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 rails-models