我有以下方法返回CourseTypes
,包括相关的courses
。 (enrolments
并不重要。)
我刚才注意到的是,如果我在第2行添加.order(:order)
,则会忽略每个where
条件。这意味着包含expired
和cancelled
课程。
当我省略.order(:order)
时,不包括这些(不需要的)课程。
为什么会这样,如何在不丢失过滤器选项的情况下正确地订购CourseTypes
数组/ activerecord关系?
def course_types
CourseType.order(:order).
includes(courses: :enrolments).
where.not("title LIKE ?" , "%Test%").
where(courses: {
expired: false,
cancelled: false,
})
end
谢谢!
更新
生成的查询没有.order(:order)
:
我已排除查询中的.enrolments
以缩短输出,并且我已在查询中翻译了db字段以匹配我在初始问题中使用的英文名称。
SQL(0.5ms)SELECT" course_types"。" id" AS t0_r0," course_types"。" title" AS t0_r1," course_types"。" description" AS t0_r2," course_types"。" created_at" AS t0_r3," course_types"。" updated_at" AS t0_r4," course_types"。" order" AS t0_r5," course_types"。"价格" AS t0_r6," course_types"。"席位" AS t0_r7," course_types"。" days" AS t0_r8," course_types"。" ue" AS t0_r9,"课程"。" id" AS t1_r0,"课程"。" titel" AS t1_r1,"课程"。" startdate" AS t1_r2,"课程"。" enddate" AS t1_r3,"课程"。"内容" AS t1_r4,"课程"。"观众" AS t1_r5,"课程"。"时间" AS t1_r6,"课程"。"范围" AS t1_r7,"课程"。"分数" AS t1_r8,"课程"。"导师" AS t1_r9,"课程"。"地点" AS t1_r10,"课程"。"活跃" AS t1_r11,"课程"。" created_at" AS t1_r12,"课程"。" updated_at" AS t1_r13,"课程"。"类别" AS t1_r14,"课程"。"已过期" AS t1_r15,"课程"。"价格" AS t1_r16,"课程"。"取消" AS t1_r17,"课程"。"笔记" AS t1_r18,"课程"。" url" AS t1_r19," course"。" course_type_id" AS t1_r20 FROM" course_types" LEFT OUTER JOIN"课程" ON"课程"。" course_type_id" =" course_types"。" id"在哪里(不是(标题LIKE'%Test%'))和"课程"。"活跃" = $ 1 AND"课程"。"已过期" = $ 2 AND"课程"。"取消" = $ 3 [["活跃"," t"],["过期"," f"],["取消&# 34;," f"]]
使用.oder(:order)
生成查询:
SQL(1.0ms)SELECT" course_types"。" id" AS t0_r0," course_types"。" title" AS t0_r1," course_types"。" description" AS t0_r2," course_types"。" created_at" AS t0_r3," course_types"。" updated_at" AS t0_r4," course_types"。" order" AS t0_r5," course_types"。"价格" AS t0_r6," course_types"。"席位" AS t0_r7," course_types"。" days" AS t0_r8," course_types"。" ue" AS t0_r9,"课程"。" id" AS t1_r0,"课程"。" titel" AS t1_r1,"课程"。" startdate" AS t1_r2,"课程"。" enddate" AS t1_r3,"课程"。"内容" AS t1_r4,"课程"。"观众" AS t1_r5,"课程"。"时间" AS t1_r6,"课程"。"范围" AS t1_r7,"课程"。"分数" AS t1_r8,"课程"。"导师" AS t1_r9,"课程"。"地点" AS t1_r10,"课程"。"活跃" AS t1_r11,"课程"。" created_at" AS t1_r12,"课程"。" updated_at" AS t1_r13,"课程"。"类别" AS t1_r14,"课程"。"已过期" AS t1_r15,"课程"。"价格" AS t1_r16,"课程"。"取消" AS t1_r17,"课程"。"笔记" AS t1_r18,"课程"。" url" AS t1_r19," course"。" course_type_id" AS t1_r20 FROM" course_types" LEFT OUTER JOIN"课程" ON"课程"。" course_type_id" =" course_types"。" id"在哪里(不是(标题LIKE'%Test%'))和"课程"。"活跃" = $ 1 AND"课程"。"已过期" = $ 2 AND"课程"。"取消" = $ 3 ORDER BY" course_types"。" order" ASC [["活跃"," t"],["已过期"," f"],["已取消&#34 ;," f"]]
如果它很重要,我使用rails v 4.2.7
。