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.

associations#find should use scoped(construct_scope).find except with custom finder_sql

#1994

There's a lot of logic duplication in associations.

When a find(options) is triggered on an association, the following things currently happen explicitly in the find method:

  • If it's got :finder_sql, a separate branch of code is taken.
  • If not (99% of the time):
    • The generated association finder_sql is merged with options[:conditions]
    • The reflection's :order is merged with options[:order]
    • Other options are reverse_merged on top of the reflection's options
    • @reflection.klass.find(*args) is finally called

This sort of merging of conditions, order, and more is already accomplished by ActiveRecord scope merging. Associations are really just named_scopes that change the main table anyway.

Association already have the construct_scope method, which can construct a complete finder scope representing that association. Using @reflection.klass.scoped(construct_scope[:find]), all of that redundant merge logic can be finally deleted out of the #find method.

Reported by David Stevenson · February 16th, 2009 @ 09:47 PM

State: stale
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Pratik

    February 17th, 2009 @ 09:55 AM

  2. David Stevenson
    David Stevenson

    Pratik - Do you still want me to contribute a patch if possible? I had this partly done, but not all tests are passing yet.

    February 17th, 2009 @ 04:05 PM

  3. Pratik
    Pratik

    Hey David,

    If you have a patch lying around, please do upload. I need to take a closer look at this though.

    Thanks.

    February 17th, 2009 @ 04:42 PM

  4. Michael Koziarski
    Michael Koziarski

    Hey David,

    Switching out the implementation of associations to be driven off named_scopes is DEFINITELY something we want to look at in the 3.0 timeframe.

    At a high level it seems like it should be possible, and be a really nice simplification. But only time will tell :)

    March 2nd, 2009 @ 05:14 AM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:41 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:41 PM