Hi,

my app is on Ruby 2.1.2 and Rails 4.1.5

like the subject states I would expect 

        current_user.account.employees.includes(:entrances).where( 
'entrances.clocked_at' => @month_range)

to return all employees (to this current_user’s account) - and any entrances

But I’m left with the employees that has entrances in the month_range

[ 
that looks like this: 

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` WHERE `employees`.`account_id` = 2 AND 
(`entrances`.`clocked_at` BETWEEN '2015-01-01' AND '2015-01-31')  ORDER BY 
employees.id, entrances.clocked_at 

]

If, however, I settle for

        current_user.account.employees.includes(:entrances)

I get all employees but than I get all the entrances loaded into memory as well

[
that looks like:

Employee Load (0.2ms)  SELECT `employees`.* FROM `employees`  WHERE 
`employees`.`account_id` = 2
Entrance Load (0.2ms)  SELECT `entrances`.* FROM `entrances`  WHERE 
`entrances`.`employee_id` IN (1, 2)
Entrance Load (0.3ms)  SELECT `entrances`.* FROM `entrances`  WHERE 
`entrances`.`employee_id` = 1 AND (`entrances`.`clocked_at` BETWEEN 
'2015-01-01' AND '2015-01-31')
Entrance Load (0.4ms)  SELECT `entrances`.* FROM `entrances`  WHERE 
`entrances`.`employee_id` = 2 AND (`entrances`.`clocked_at` BETWEEN 
'2015-01-01' AND '2015-01-31’)

]

The correct statement is

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 

but how do I write that “the Rails way”


cheers
Walther

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/1B988F15-652E-495F-A2FB-123B234AB0C4%40diechmann.net.
For more options, visit https://groups.google.com/d/optout.

Reply via email to