New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sum fail on includes #2896
sum fail on includes #2896
Conversation
Hi, thanks for the patch. Rather than adding a def references_eager_loaded_table?(sql = to_sql) And then change |
@@ -871,6 +871,14 @@ class EagerAssociationTest < ActiveRecord::TestCase | |||
end | |||
end | |||
|
|||
def test_eager_association_loading_for_sum | |||
categories = Category.includes(:authors) | |||
assert_nothing_raised do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need for assert_nothing_raised
here, the test will fail if an exception is raised.
I'll rebase and change commit according to your other comments . |
I recall discussion at one point about deprecating this behavior. Parsing SQL strings is a little mad when you can just use Category.joins(:authors) and get the same result no? Perhaps we could deprecate this for 3.2 and remove in 3.3? |
Sure, this is mad. |
@lysenko you said originally "to_sql and and sql generated by Relation#sum is two different things.", so I am suggesting that you pass "sql generated by Relation#sum" to @NZKoz I agree with you. Though we should still accept a patch for this as it is (apparently) a regression. But I am up for deprecating. |
@jonleighton totally, apply the patch and all that, let's just think about how/if to clear this up for the future. @lysenko include and joins are two different things. include means "join and also load objects from the results from the join table", that makes no sense when you're issuing a count query. Having said that you'd probably nuke some pagination gems, but this ambiguity is pretty harmful at present. |
@jonleighton, @NZKoz, def Category.cool_relation Then in controller A: And in controller B: Is it something bad? |
@lysenko Is this still needed? |
It is not needed anymore, according to @jonleighton : a2dab46 |
Sum and other calculations generate wrong sql when used with includes and called on included table.
The following line will raise sql exception.
Category.includes(:authors).sum('authours.id')
The problem was in Relation#references_eager_loaded_tables?, it checks that Relation#to_sql references eager loaded tables, but to_sql and and sql generated by Relation#sum is two different things.