嘿。在我的 Controller /索引操作中,我使用以下查询:
@course_enrollments = current_user.course_enrollments
这就是我的 table 的样子。它正在引用类(class)表。类(class)表有一个“标题”列。
create_table "course_enrollments", :force => true do |t|
t.integer "user_id", :null => false
t.integer "course_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
我希望能够在索引 View 中按类(class)对 course_enrollments 进行排序。此外,我想在我的模型中做一个default_scope,如下所示:
default_scope :order => 'title asc'
有什么建议吗?感谢您的宝贵时间
最佳答案
要对父模型进行排序,请按照Rails 3 sorting through parent association中的说明进行操作。 ,即:
CourseEnrollments.joins(:course).order('courses.title')
对于默认范围,您应该能够使用:
default_scope joins(:course).order('courses.title')
关于ruby-on-rails - 在模型表上使用 default_scope 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5130399/