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

#2976

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: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: none

Activity

  1. Will Bryant
    Will Bryant
    • Assigned user set to Frederick Cheung

    Here's a test case showing the problem.

    Frederick, it looks to me like in the case where there are conditions on the source table, we can't do the loading in two separate steps, we'd have to use a single query. Would it be better to always use a single query for the join table and the source table parts?

    August 13th, 2009 @ 02:05 AM

  2. Frederick Cheung
    Frederick Cheung

    That's certainly true. Something that has been on my todo list for ages has been to rewrite preload of has many/one through to not do it in two steps. I don't think it's difficult, just a little fiddly generating the join statements if you want to cover all of the various cases (and I'd like to do it without too much code duplication between this and the old style include and the normal load of hmt)

    August 13th, 2009 @ 11:08 AM

  3. Will Bryant
    Will Bryant

    Hi Frederick,

    is this something we're working on for the 2-3-stable series, or do you plan to leave it till Rails 3.0?

    January 2nd, 2010 @ 10:50 PM

  4. Frederick Cheung
    Frederick Cheung

    Ideally I would have worked on this a while ago but haven't had much time to give to rails over the past 3 months. It looks like there has been some refactoring of has many/one through so that generating the correct join clauses is less fiddly / error prone.

    January 4th, 2010 @ 10:47 AM

  5. Will Bryant
    Will Bryant
    • Tag changed from 2.3.3, :include, activerecord, bug, has_one, preload, through to 3.0, :include, activerecord, bug, has_one, preload, through

    Just to confirm, this test is still failing in master, so broken for 3.0.

    May 26th, 2010 @ 05:23 AM

  6. Jon Leighton
    Jon Leighton
    • Assigned user changed from Frederick Cheung to Aaron Patterson
    • Tag changed from 3.0, :include, activerecord, bug, has_one, preload, through to 3.0, :include, activerecord, bug, has_one, patch, preload, through
    • Importance changed from to

    Here is an updated version of Will's test which applies cleanly to current master (the previous version did not). I have run it and can confirm that it passes, so this ticket can be closed. (I've edited the commit message to be more relevant to the current situation.)

    December 19th, 2010 @ 03:18 PM

  7. Repository
    Repository
    • State changed from new to resolved

    (from [c6db37e69b1ff07f7ad535d4752d0e6eb2d15bff]) Don't allow a has_one association to go :through a collection association [#2976 state:resolved] https://github.com/rails/rails/commit/c6db37e69b1ff07f7ad535d4752d0...

    December 23rd, 2010 @ 11:20 PM

  8. Repository
  9. ssupreme11
    ssupreme11

    Its my first time to visit this site and as I was exploring I cant believe that this site was made up of a very informative articles that you should try to have compliment with so as what I am doing now I really love to look forward with more interesting information on this site.. Affordable Dissertation

    May 10th, 2011 @ 10:38 PM

  10. Kira Steels