If I have:
class Customer
has_one :active_debit_balance_event, :through => :consumers, :source => :events, :class_name => 'Event',
:conditions => {:events => {:active => true, :type => 'DebitBalanceEvent'}}
end
Then Customer.find(370).active_debit_balance_event correctly
uses:
Customer Load (0.6ms) SELECT * FROM `customers` WHERE (`customers`.`id` = 370)
Event Load (5.7ms) SELECT `events`.* FROM `events` INNER JOIN `consumers` ON `events`.consumer_id = `consumers`.id WHERE ((`consumers`.customer_id = 370) AND ((`events`.`type` = 'DebitBalanceEvent' AND `events`.`active` = 1)))
However if I preload using Customer.find(370, :include =>
:active_debit_balance_event) then instead we see:
Customer Load (0.5ms) SELECT * FROM `customers` WHERE (`customers`.`id` = 370)
Consumer Load (0.6ms) SELECT `consumers`.* FROM `consumers` WHERE (`consumers`.customer_id = 370) ORDER BY start_date desc
Event Load (27.3ms) SELECT `events`.* FROM `events` WHERE (`events`.consumer_id = 377) ORDER BY created_at DESC
Note that there are no conditions on the events query. The
resulting loaded has_one association actually returns a collection
(looks like an array - ie. like a has_many).