如何在AREL中检查连接表关联?

时间:2012-07-25 13:28:16

标签: ruby-on-rails where arel

基本上我正在寻找这种工作方法的AREL版本:

@jobs.select{|a| !a.assignments.present? }

假设AREL或where语句更准确更快。如何比现在更快地写出

2 个答案:

答案 0 :(得分:2)

假设Job has_many assignments

@jobs.where('id in (select j.id from jobs j inner join assignments a on a.job_id=j.id)')

答案 1 :(得分:1)

Job.joins(:assignments).where('assignments.present' => false)

执行这个sql:

 SELECT "jobs".* FROM "jobs" INNER JOIN "assignments" ON "assignments"."job_id" = "jobs"."id" WHERE "assignments"."present" = 't'