如何使用活动记录形成以下查询?
SELECT c.*
FROM `course_enrollments` ce JOIN courses c ON ce.course_id = c.id
WHERE ce.created_at
BETWEEN '2000-01-01' and '2012-01-01' [AND ANOTHER POSSIBLE CONDITION]
GROUP BY c.id
我希望能够做到这样的事情:(我知道下面的内容不正确,但我只是想展示一般的例子)
courses = Course.joins(:course_enrollments).where('course_enrollments.created_at' => params[:start_date]..params[:end_date]).group('courses.id')
if some_condition
courses = courses.where(:some_field => 1)
end