Skip to content
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

Fixed a problem where sum used with a group was not returning a Hash. #14803

Merged
merged 1 commit into from
May 22, 2014

Conversation

kuldeepaggarwal
Copy link
Contributor

\cc @senny

@rafaelfranca
Copy link
Member

There is already a PR for this. Could you check if it was merged? Thanks
On Apr 18, 2014 5:39 PM, "Kuldeep Aggarwal" notifications@github.com
wrote:

\cc @senny https://github.com/senny

You can merge this Pull Request by running

git pull https://github.com/kuldeepaggarwal/rails null_relation_sum_fix

Or view, comment on, or merge it at:

#14803
Commit Summary

  • Fixed a problem where sum used with a group was not returning a
    Hash.

File Changes

Patch Links:


Reply to this email directly or view it on GitHubhttps://github.com//pull/14803
.

@kuldeepaggarwal
Copy link
Contributor Author

@rafaelfranca It has already been merged. here it is #14773

@matthewd
Copy link
Member

Hmm. @eric-chahin, I thought you specifically checked the behaviour of sum.

Are there any other methods we should be addressing at the same time?

@rafaelfranca
Copy link
Member

Do we can close this one right?
On Apr 18, 2014 6:36 PM, "Kuldeep Aggarwal" notifications@github.com
wrote:

@rafaelfranca https://github.com/rafaelfranca It has already been
merged. here it is #14773 #14773


Reply to this email directly or view it on GitHubhttps://github.com//pull/14803#issuecomment-40846644
.

@kuldeepaggarwal
Copy link
Contributor Author

@matthewd I think this was the last method in NullRelation which might cause trouble.

@matthewd
Copy link
Member

@rafaelfranca no; that was for count, this is for sum.

@eric-chahin
Copy link
Contributor

I was under the impression that it was only count that should return the hash and not sum via https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb#L17

@matthewd
Copy link
Member

Okay, well by my reading of https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb#L213, we're still not there...

I see no reason that average/minimum/maximum (or more generally, anything that ends up inside calculate -> perform_calculation) won't end up returning a hash. Am I missing something?

@kuldeepaggarwal
Copy link
Contributor Author

@matthewd: But these(avg/max/min) methods are not defined in the NullRelation.

@eric-chahin
Copy link
Contributor

@matthewd Would we actually need to add those into NullRelation?

    def count(*)
      calculate :count, nil
    end

    def sum(*)
      calculate :sum, nil
    end

    def average(*)
      calculate :average, nil
    end

    def minimum(*)
      calculate :minimum, nil
    end

    def maximum(*)
      calculate :maximum, nil
    end

    def calculate(operation, _column_name, _options = {})
      if [:count, :sum, :average, :minimum, :maximum].include? operation
        group_values.any? ? Hash.new : 0
      else
        nil
      end
    end

Unless my initial assumption was correct that count is the special exception! 😬

@eric-chahin
Copy link
Contributor

@matthewd: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/calculations.rb#L83
It seems like maximum was not intended return a hash? 😕

@kuldeepaggarwal
Copy link
Contributor Author

@matthewd: any suggestions?

@matthewd
Copy link
Member

@eric-chahin you're misreading: the preceding line is pulling a value from the hash with the key "Drake".

@kuldeepaggarwal well, as you point out, they're currently not defined here at all. So we don't have the problem that they're returning zero. 😄

But I don't see any reason to expect them to be missing... as far as I understand, NullRelation is intended to act just like any other relation.. so it seems those methods should in fact be here.

That said, while they can all return an empty hash when grouped, they should not return zero in the ungrouped situation... the average, minimum, and maximum of an empty set is nil, not 0.

@kuldeepaggarwal
Copy link
Contributor Author

@matthewd Should I add those methods too?

@srbiv
Copy link

srbiv commented May 13, 2014

I'm running into this issue, is anyone still working on this?

@kuldeepaggarwal
Copy link
Contributor Author

@matthewd any suggestions?

@matthewd
Copy link
Member

@kuldeepaggarwal sorry.. yes, please add those.

…m` used

with a grouping was not returning a Hash.
@kuldeepaggarwal
Copy link
Contributor Author

@matthewd Added other aggregate methods and its test cases.

@matthewd matthewd merged commit 6d36c1d into rails:master May 22, 2014
matthewd added a commit that referenced this pull request May 22, 2014
Fixed a problem where `sum` used with a `group` was not returning a Hash.
@kuldeepaggarwal
Copy link
Contributor Author

Thanks 💚 💙 💛 💜

matthewd added a commit that referenced this pull request May 22, 2014
Fixed a problem where `sum` used with a `group` was not returning a Hash.

Conflicts:
	activerecord/CHANGELOG.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants