This project is archived and is in readonly mode.
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 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 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 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 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 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 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 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 March 6th, 2009 @ 09:06 PM
- Title changed from [PATCH] making ActiveRecord #with_scope merge :selects to Making ActiveRecord #with_scope merge :selects
-
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 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>
People watching this ticket
Attachments
Referenced by
- 2918 ActiveRecord::Base finders select merging EDIT: this is duplicate of #1295, but my patch fixes also...