This project is archived and is in readonly mode.
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
-
Will Bryant August 13th, 2009 @ 02:05 AM
- 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?
-
Frederick Cheung August 13th, 2009 @ 11:08 AM
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)
-
Will Bryant January 2nd, 2010 @ 10:50 PM
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?
-
Frederick Cheung January 4th, 2010 @ 10:47 AM
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.
-
Will Bryant May 26th, 2010 @ 05:23 AM
- 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.
-
Jon Leighton December 19th, 2010 @ 03:18 PM
- 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.)
-
Repository December 23rd, 2010 @ 11:20 PM
- 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...
-
Repository December 23rd, 2010 @ 11:20 PM
(from [b79823832e6cd30a9f14f97ffdf1642d4d63d4ea]) Verify that has_one :through preload respects the :conditions [#2976 state:resolved] https://github.com/rails/rails/commit/b79823832e6cd30a9f14f97ffdf16...
-
ssupreme11 May 10th, 2011 @ 10:38 PM
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
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
Attachments
Referenced by
- 2977 has_one :through preload drops conditions Duplicate of #2976
- 2976 has_one :through preload drops conditions (from [c6db37e69b1ff07f7ad535d4752d0e6eb2d15bff]) Don't a...
- 2976 has_one :through preload drops conditions (from [b79823832e6cd30a9f14f97ffdf1642d4d63d4ea]) Verify ...