This project is archived and is in readonly mode.

#1994 ✓stale
David Stevenson

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

Reported by David Stevenson | February 16th, 2009 @ 09:47 PM | in 3.x

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.

Comments and changes to this ticket

  • Pratik

    Pratik February 17th, 2009 @ 09:55 AM

    • Assigned user set to “Pratik”
  • David Stevenson

    David Stevenson February 17th, 2009 @ 04:05 PM

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

  • Pratik

    Pratik February 17th, 2009 @ 04:42 PM

    Hey David,

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

    Thanks.

  • Michael Koziarski

    Michael Koziarski March 2nd, 2009 @ 05:14 AM

    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 :)

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:41 PM

    • 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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 04:41 PM

    • State changed from “open” to “stale”

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>

Pages