This project is archived and is in readonly mode.

#2977 ✓duplicate
Will Bryant

has_one :through preload drops conditions

Reported by Will Bryant | July 31st, 2009 @ 05:25 AM

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).

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages