1

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

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

假设 AREL 或where语句会更正确更快。一个人怎么会比我现在写得更快?

4

2 回答 2

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)')
于 2012-07-25T13:39:36.973 回答
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'
于 2012-07-25T14:20:27.603 回答