我有3种型号:
class Student < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :courses, through: :student_enrollments
end
class Course < ActiveRecord::Base
has_many :student_enrollments, dependent: :destroy
has_many :students, through: :student_enrollments
end
class StudentEnrollment < ActiveRecord::Base
belongs_to :student
belongs_to :course
end
我希望在“课程”表中查询课程列表,该列表在与某个学生相关联的“学生录取”表中不存在。
我发现也许使用Left Join是可行的方法,但似乎rails中的joins()仅接受一个表作为参数。我认为可以执行的SQL查询是:
SELECT *
FROM Courses c LEFT JOIN StudentEnrollment se ON c.id = se.course_id
WHERE se.id IS NULL AND se.student_id = <SOME_STUDENT_ID_VALUE> and c.active = true
如何在Rails 4中执行此查询?
任何输入表示赞赏。
se.student_id = <SOME_STUDENT_ID_VALUE>
是不可能的吗?