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.

Accessing associations via x_ids no longer uses association conditions

#6153

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.

Reported by Yuval Kordov · December 11th, 2010 @ 11:51 PM

State: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Yuval Kordov
    Yuval Kordov

    Forgot to add that this is using Ruby 1.9.2 and the mysql2 gem.

    December 12th, 2010 @ 10:58 PM

  2. Yuval Kordov
  3. 2kan
    2kan
    • 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

    December 19th, 2010 @ 02:59 PM

  4. Jon Leighton
    Jon Leighton
    • 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 :conditions or :order options 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.

    December 22nd, 2010 @ 10:37 AM

  5. Jon Leighton
  6. Repository
    Repository
    • 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 Accessing associations via x_ids no longer uses association conditions state:resolved]

    This reverts commit 373b053dc8b99dac1abc3879a17a2bf8c30302b5.

    Conflicts:

    activerecord/lib/active_record/associations.rb
    

    https://github.com/rails/rails/commit/1619c2435b2b9c821b2b0dcab9624...

    December 23rd, 2010 @ 11:21 PM