基本上我正在寻找这种工作方法的AREL版本:
@jobs.select{|a| !a.assignments.present? }
假设AREL或where
语句更准确和更快。如何比现在更快地写出 ?
答案 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'