This project is archived and is in readonly mode.

#1295 ✓stale
FiXato

Making ActiveRecord #with_scope merge :selects

Reported by FiXato | October 30th, 2008 @ 09:47 AM | in 3.x

When adding :select to scopes, only the first one defined gets used, instead of all the scoped :select arguments being merged together. Since in Edge Rails the :joins now properly merge together through scopes, it would seem expected behaviour for the :select to do this as well.

The attached patch allows scopes the merge their :select's so the following examples:

Author.with_scope(:find => { :select => 'authors.name'}) do
  scoped_authors = Author.with_scope(:find => { :select => 'authors.id'}) do
    Author.find(:all, :conditions => {:id => 1})
  end
end

and:

Author.scoped(:select => 'authors.id').scoped(:select => 'authors.name', :conditions => "authors.id = #{author.id}")

will both generate the following SQL:

SELECT authors.id,authors.name FROM "authors" WHERE (authors.id = 1)  LIMIT 1

Comments and changes to this ticket

  • FiXato

    FiXato October 31st, 2008 @ 08:58 AM

    • Title changed from “making with_scope merge :selects” to “[PATCH] making ActiveRecord #with_scope merge :selects”
    • Tag changed from 2.2, :select, active, activerecord, scope, with_scope to 2.2, :select, active, activerecord, patch, scope, with_scope
  • David Stevenson

    David Stevenson December 29th, 2008 @ 05:21 AM

    This is a very interesting ticket, which I bet many of us have thought about before. Merging selects sounds difficult, but not impossible like merging GROUP BY, and it won't work in some cases.

    I'd try for an approach like the way we merge :joins, removing duplicates to avoid selecting "users.,users." and getting those columns back twice.

  • Pascal Ehlert

    Pascal Ehlert January 1st, 2009 @ 06:12 PM

    Whoa, I've recently posted to rails-core mailing list asking for the reason why selects aren't being merged..

    Although it may be hard, it's not harder than merging joins for instance so we should definitely give it a try!

    +1 on this and I'm starting to hack on this as well.

  • Pascal Ehlert

    Pascal Ehlert January 2nd, 2009 @ 02:55 AM

    So to give you a quick update what I found hardest about merging two select statements is to get eventual DISTINCT (ON) clauses right..

    I doubt this is possible without an SQL parser at all, but maybe someone who is better at SQL than me can have a look.. This would be a feature I'd definitely appreciate.. I've got a patch with everything but this ready..

  • Pascal Ehlert

    Pascal Ehlert January 2nd, 2009 @ 12:15 PM

    So here comes my take on the problem, now ignoring eventual problems with DISTINCT ON..

    If anyone should ever use this it will be obvious that complex DISTINCT ON with subselects in scopes are likely to generate invalid SQL.

    However it's not problem to specify them in the actual find.

    Let me know what you think, all tests are passing.

  • Amos King

    Amos King January 2nd, 2009 @ 09:12 PM

    I'm willing to try this, but why not just have a different named scope for you select clause?

    +0

  • Pascal Ehlert

    Pascal Ehlert January 3rd, 2009 @ 01:14 AM

    Because I found that chaining scopes often makes sense even with select clauses, especially internally from plugins (I'd need it for a db translations plugin which selects and aliases fields from a joined translations table)

  • Pratik

    Pratik March 6th, 2009 @ 09:06 PM

    • Title changed from “[PATCH] making ActiveRecord #with_scope merge :selects” to “Making ActiveRecord #with_scope merge :selects”
  • qoobaa

    qoobaa July 21st, 2009 @ 10:15 AM

    I couldn't find this patch, so I've made a duplicate #2918 - sorry. My patch also fixes the problems with DISTINCTs. I've also included the tiny patch to remove "safe_to_array" method from base.rb: #2916.

  • 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 @ 05:04 PM

    • State changed from “new” to “open”
    • Tag changed from 2.2, :select, active, activerecord, patch, scope, with_scope to 22, active, activerecord, patch, scope, select, with_scope
    • 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 @ 05:04 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>

Referenced by

Pages