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/7a07cb60-38f1-4e6a-9dbb-b028d48b77f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to