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.

Duplicate association conditions are generated when using a named_scope on an association

#4634

Probably not a bug, as it does not change the behavior. But I spent the afternoon tracking down why my association conditions were being applied twice in the query, so hopefully this ticket will prevent others from wasting an afternoon.

#1960 introduced this behavior.

So when you do Foo.my_assoc.my_scope:
When my_scope gets constructed it is within the scope of my_assoc, so it saves that scope's finder conditions internally. When you then evaluate the scope and the association to fetch records, my_assoc and my_scope both apply the association finder conditions separately so you end up with it twice in your query.

I'm guessing this is fixed in rails 3 by arel, but I have not checked.

Reported by Joseph Palermo · May 18th, 2010 @ 12:52 AM

State: committed
Milestone: 2.3.10
Assigned to: Aaron Patterson Aaron Patterson
Importance: High

Activity

  1. gurenski
    gurenski

    Thank you for noticing this issue. I'm seeing the exact same thing and its quite annoying. Any suggestions on how to fix this without having to include what's in the named scope within each instance?

    May 19th, 2010 @ 02:02 AM

  2. Rizwan Reza
    Rizwan Reza
    • No changes were found…

    May 21st, 2010 @ 12:47 AM

  3. Marcelo Giorgi
    Marcelo Giorgi
    • Assigned user set to Santiago Pastorino
    • Tag changed from rails 2.3.5, associations, named_scope to rails 2.3.5, associations, named_scope, patch
    • Importance changed from to High

    Here I attach a fix for this duplication of conditions when applied to an AssociationCollection object.

    This patch applies cleanly on 2-3-stable.

    Please let me know any comment you may have.

    August 26th, 2010 @ 11:11 PM

  4. Santiago Pastorino
    Santiago Pastorino
    • Milestone set to 2.3.9
    • State changed from new to open

    August 27th, 2010 @ 05:28 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.3.9 to 2.3.10

    August 30th, 2010 @ 02:28 AM

  6. Santiago Pastorino
    Santiago Pastorino
    • Assigned user changed from Santiago Pastorino to Aaron Patterson

    September 28th, 2010 @ 07:19 PM

  7. Repository
    Repository

    (from [9b78af95be01534b3f1a4187024d803313c0ee1f]) Remove duplication of conditions generated for associations when used in conjunction with named_scopes [#4634 state: resolved] http://github.com/rails/rails/commit/9b78af95be01534b3f1a4187024d80...

    September 30th, 2010 @ 05:11 PM

  8. Aaron Patterson
    Aaron Patterson
    • State changed from open to committed

    I've applied this patch. Thanks.

    September 30th, 2010 @ 05:11 PM

  9. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.

    Automatic cleanup of spam.

    November 8th, 2010 @ 08:50 AM