在Google群组中,我添加了我的问题 - here infact!
我的问题是如何表达这种外部联接'Rails方式':
SELECT
`employees`.`id` AS t0_r0, `employees`.`name` AS t0_r1, `employees`.`last_seen` AS t0_r2, `employees`.`created_at` AS t0_r3, `employees`.`updated_at` AS t0_r4, `employees`.`punch_clock_id` AS t0_r5, `employees`.`account_id` AS t0_r6, `employees`.`born_at` AS t0_r7, `entrances`.`id` AS t1_r0, `entrances`.`employee_id` AS t1_r1, `entrances`.`clocked_at` AS t1_r2, `entrances`.`created_at` AS t1_r3, `entrances`.`updated_at` AS t1_r4, `entrances`.`entrance_type` AS t1_r5
FROM
`employees`
LEFT OUTER JOIN
`entrances` ON `entrances`.`employee_id` = `employees`.`id` AND (`entrances`.`clocked_at` BETWEEN '2015-01-01' AND '2015-01-31’)
WHERE
`employees`.`account_id` = 2
我的第一次接受是:
current_user.account.employees.includes(:entrances).where( 'entrances.clocked_at' => @month_range)
但这只会为员工提供入口(除非我放弃了声明的'where'部分,在这种情况下我会得到所有员工(以及所有250K入口!!!)
答案 0 :(得分:0)
如果您希望包含employees
而不包含entrances
,则必须通过调用where
在entrances.id IS NULL
语句中明确包含该案例。
current_user.account.employees.includes(:entrances).where( 'entrances.id IS NULL OR entrances.clocked_at = ?', @month_range)
根据@month_range
包含的内容,最后一部分可能会略有不同。