Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

has_one :through preload drops conditions

#2977

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

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

State: duplicate
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Will Bryant
    Will Bryant

    Hmm, this got double-posted for some reason - should be just ticket 2976. Probably my fault, could someone with permissions please close?

    July 31st, 2009 @ 05:57 AM

  2. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to duplicate

    Duplicate of #2976

    July 31st, 2009 @ 11:54 AM