This project is archived and is in readonly mode.
ActiveRecord: :select is wiped when doing a join
-
Oleg
I'm bumping this again with a test
class AssociationsJoinModelTest < ActiveRecord::TestCase def test_my_little_test p = Post.find(:all, :select => 'author_id, count(comments_count) as my_comment_count', :group => 'author_id', :include => :comments) assert p.first.respond_to?(:my_comment_count) p = Post.find(:all, :select => 'author_id, count(comments_count) as my_comment_count', :group => 'author_id', :include => :comments, :order => 'comments.type') assert p.first.respond_to?(:my_comment_count), :message => 'joining kills my custom select' end endAnd this is generated SQL.
First find call (Notice that eager loading is whacked too now, previous revisions worked fine though):
SELECT author_id, count(comments_count) as my_comment_count FROM `posts` GROUP BY author_id SELECT `comments`.* FROM `comments` WHERE (`comments`.post_id IN (NULL)) ORDER BY bodySecond find call:
SELECT `posts`.`id` AS t0_r0, `posts`.`author_id` AS t0_r1, `posts`.`title` AS t0_r2, `posts`.`body` AS t0_r3, `posts`.`type` AS t0_r4, `posts`.`comments_count` AS t0_r5, `posts`.`taggings_count` AS t0_r6, `comments`.`id` AS t1_r0, `comments`.`post_id` AS t1_r1, `comments`.`body` AS t1_r2, `comments`.`type` AS t1_r3 FROM `posts` LEFT OUTER JOIN `comments` ON comments.post_id = posts.id GROUP BY author_id ORDER BY comments.type -
Oleg
Ignore my comment about eager loading being borked, I forgot I needed to do :select => 'id' as well. Issue still stands, however.
-
Frederick Cheung
That's the way it's always been as far as I know. Sounds like you just want :joins instead of :include.
-
Oleg
Well, damn... You are correct. :joins actually works in this case. No reason why :include shouldn't work as well though. Rather misleading thinking that they can be used somewhat interchangeably.
No idea why I didn't try :joins right away.
Thanks :)
-
Frederick Cheung
I fear you've rather got the wrong end of the stick. :joins just write out all those joins for you, nothing else (prior to 2.0 you'd just write out the joins yourself). :include does that and then does a lot of wiring behind the scenes so that all the associations are loaded in one go. It's that fancy wiring that requires messing with :select. It's a waste to use :include when all you needed was :joins
-
James Kebinger
just noticed this - this bug actually is the cause of a bug I just added to #306 where eager loading relies on a column aliased as _parent_id in the :select that get clobbered because there's an :include specified
-
kos
- Tag set to activerecord, edge, joins
In case you still looking for solution (though this post is 3 month old) you can consider using select_with_include plugin (http://assertbuggy.blogspot.com/... or take a look at the following patch (http://dev.rubyonrails.org/attac...>
I faced the same problem not long ago. Unfortunately solutions mentioned above didn't work for me... Still searching :(
-
Andy Waite
It would be helpful if ActiveRecord generated an exception if you attempted to use :select in combination with :include.
-
Pratik
- Assigned user set to Frederick Cheung
-
Frederick Cheung
- State changed from new to wontfix