Kevin,

When I read: 
SELECT  `call_centers`.* FROM `call_centers` WHERE `call_centers`.`id` = @id 
LIMIT 1
I recognize the query insert @id as a flag of an error.  My guess is that 
your application is not recognizing admins.call_center for some reason.  
Check your models, look at your associations--without provisions you need 
to follow Rails conventions. Rails documentation regarding joins with and 
without established association I find lacking, and go to your database and 
try to perform LEFT JOIN there.
Liz

On Saturday, August 8, 2015 at 11:23:37 AM UTC-4, Kévin Lesénéchal wrote:
>
> Hey everyone,
>
> I've got two models: Admin and CallCenter. An Admin may belong to a 
> CallCenter (the field is nullable). To keep history, Admins and CallCenters 
> have a deleted field with, for both of them:
>
> default_scope { where(deleted: false) }
>
> Now, I want to implement Admin.all_detailed which will retrieve all 
> non-deleted admins with their call center (LEFT JOIN). Simply doing a 
> eager_load will make a join with the default_scope condition, thus, 
> non-deleted admins which belong to a deleted call center will be shown not 
> having one. The unscoped_associations gem didn't solve this issue. 
> Therefor, I did this:
>
> def self.all_detailed
>   return joins("LEFT JOIN call_centers ON call_centers.id = 
> admins.call_center")
> end
>
> But now, when accessing the call center method of each admin, it triggers 
> a query:
>
> SELECT  `call_centers`.* FROM `call_centers` WHERE `call_centers`.`id` = 
> @id LIMIT 1
>
> I figured out the query doesn't *SELECT* the call center's fields:
>
> SELECT `admins`.* FROM `admins` LEFT JOIN call_centers -- ...
>
> So I added the SELECT fields:
>
> def self.all_detailed
>   return select("admins.*, call_centers.*")
>          .joins("LEFT JOIN call_centers ON call_centers.id = 
> admins.call_center")
> end
>
> But a query is still triggered for every access to the call_center method 
> of Admin. It seems like Rails does not know how to link the SELECT with 
> the Admin's association, but I don't see what else I can do.
>
> Thanks for your help!
>

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/86f72b9f-36db-44f3-9ed4-ba736571fd8f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to