This project is archived and is in readonly mode.

Accessing associations via x_ids no longer uses association conditions
Reported by Yuval Kordov | December 11th, 2010 @ 11:51 PM
I'm upgrading my Rails app from 2.3.5 to 3.0.3 and have found the following:
user.rb
has_many :relationships
has_many :contacts, :through => :relationships, :conditions
=> {:relationships => {:status => "1"}}
In Rails 2.3.5, I could call user.contact_ids and it would run through the entire condition:
user.contact_ids =>
SELECT users.id FROM users INNER JOIN
relationships ON users.id =
relationships.contact_id WHERE
((relationships.user_id = 2) AND
((relationships.status = 1)))
In Rails 3.0.3, it seems to be ignoring the parameters altogether.
user.contact_ids =>
SELECT DISTINCT relationships.contact_id FROM
relationships WHERE
(relationships.user_id = 2)
Hopefully I'm missing something obvious here, but this change has put a serious wrench in my code, where I was using _ids associations for efficiency when I only ever needed IDs.
Comments and changes to this ticket
- 
            
         Yuval Kordov December 12th, 2010 @ 10:58 PMForgot to add that this is using Ruby 1.9.2 and the mysql2 gem. 
- 
            
         
- 
            
         2kan December 19th, 2010 @ 02:59 PM- Tag changed from rails 3.0.3, associations, has_many to rails 3.0.3, associations, bug, has_many_through
 Have the same bug in edge. It works correct for has_many but incorrect for has_many :through 
- 
         Jon Leighton December 22nd, 2010 @ 10:37 AM- State changed from new to open
- Assigned user set to Aaron Patterson
- Importance changed from  to Low
 This problem was introduced by this optimisation: https://github.com/rails/rails/commit/373b053dc8b99dac1abc3879a17a2... The optimisation could still work, but we'd need to be very cautious about when it's applied. For example, it could never be applied when there are :conditionsor:orderoptions on the reflection, through_reflection, or source_reflection.My preference is to just revert the optimisation, I don't think it's worth muddying the code to tests for all these conditions to apply a small optimisation in a very specific case. I've attached a patch with 1) a test which fails on master and 2) a commit which reverts the above, causing the test to pass again. 
- 
         Jon Leighton December 22nd, 2010 @ 10:38 AM- no changes were found...
 
- 
         Repository December 23rd, 2010 @ 11:21 PM- State changed from open to resolved
 (from [1619c2435b2b9c821b2b0dcab9624dbb6b23eaaa]) Revert "Optimize _ids for hm:t with belongs_to source". The optimisation has too many edge cases, such as when the reflection, source reflection, or through reflection has conditions, orders, etc. [#6153 state:resolved] This reverts commit 373b053dc8b99dac1abc3879a17a2bf8c30302b5. Conflicts: activerecord/lib/active_record/associations.rbhttps://github.com/rails/rails/commit/1619c2435b2b9c821b2b0dcab9624... 
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
- 
         6153 
          Accessing associations via x_ids no longer uses association conditions
        (from [1619c2435b2b9c821b2b0dcab9624dbb6b23eaaa])
Revert ... 6153 
          Accessing associations via x_ids no longer uses association conditions
        (from [1619c2435b2b9c821b2b0dcab9624dbb6b23eaaa])
Revert ...
 Jeremy Kemper
      Jeremy Kemper
 Jon Leighton
      Jon Leighton
 Michał Łomnicki
      Michał Łomnicki
 Piotr Sarnacki
      Piotr Sarnacki
 Yuval Kordov
      Yuval Kordov