This project is archived and is in readonly mode.
ActiveRecord#calculate broken for multiple fields in :group option
-
josh
- State changed from new to stale
-
Sebastian
+1 for fixing this issue.
-
Seth Ladd
+1 for addressing this issue. or being much more explicit in the docs. I would want multiple group by support, achieved via an array as the original post suggests.
-
Alex
+1 for having a way to group by multiple fields.
-
jdwyah
+1 In the meantime, see my ugly hack: poor_mans_group_by.rb http://pastie.org/471055 coalesce deals with nulls and delimitters are chosen to be unicode values unlikely to be used in your code.
-
Sebastian
Couldn't we use the returned row-hash (minus count_all) as the key?
-
Matt White
+1 for getting this fixed...
-
Scott Brown
Here's another +1
-
Jacob Kjeldahl
- Tag set to activerecord, calculations, group, patch
I have found a patch by Eric Lindvall here http://tinyurl.com/luas8x it is an attachment to another lighthouse ticket which I am unable to find.
The patch works against ActiveRecord v2.3.2 (For v2.1.0 check this gist http://gist.github.com/142343)
I have corrected a tiny error in it and attached it to this ticket.
From the test:
@@@ Ruby def test_should_group_by_multiple_fields
c = Account.count(1, :group => [:credit_limit, :firm_id]) assert_equal [[50, nil], [50, 1], [50, 6], [53, 9], [55, 6], [60, 2]], c.keys end -
Gavin Stark
Another +1. This patch would be very useful.
-
Chris Taggart
Another +1.
-
James Brooks
+1
-
Paul Holzberger
+1
for those looking for temporary support of this, there is a plugin i tracked down from some other references in this thread: http://github.com/patientslikeme/multi_field_group_by
-
Rizwan Reza
- No changes were found…
-
Alex
- Importance changed from to Low
Just created a patch that resolves this issue https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets.... It also works with COALESCE and other SQL functions. Please help verify the patch (see link above) if you guys are still interested in a solution.
-
Alex
Uploading a patch here in case any of you guys want to verify it.
-
Sebastian
I verified your patch and made it work against the 3-0-stable branch. I also added one more test case for using sql-functions in the group clause.
-
Santiago Pastorino
- State changed from stale to open
- Milestone set to 3.0.2
- Assigned user set to Aaron Patterson
-
Alex
I updated the patch to work against master branch.
-
Aaron Patterson
Hi everyone! I'd really like to push rails to do less SQL parsing. Parsing SQL is a dangerous game.
If we could support this via a list passed to the :group clause (as @gix suggests), would that be OK with everyone?
-
Alex
I agree that is better. The only reason it was done that way was because the documentation when this issue was first opened hinted that the correct usage was to pass in a SQL frag. I updated the patches for master and 2-3-stable such that there is no SQL parsing. They are attached.
-
Alex
Forgot to attach 2-3-stable patch.
-
Aaron Patterson
@Alex excellent, thank you. I'll apply and push after verifying everything. Thanks for the patches!
-
Alex
Great news! Thank you.
-
Ludovic Gasc
Are you sure this bug is fixed ?
I've tested with AR 3.0.3+your patch and AR 3.0.4.rc built with the Git repository, the SQL query is correct but not the response of the method.
code:
pp SavedForm.count(:group => 'agent_id, code_dispo')
result:
DEBUG -- : SQL (1.4ms) SELECT COUNT(*) AS count_all, agent_id, code_dispo AS agent_id_code_dispo FROM
saved_formsGROUP BY agent_id, code_dispo {"bte_vocale"=>3, "CB"=>1, "nc"=>1, "pasok"=>4, "wln"=>2, "ok"=>3}# gem list *** LOCAL GEMS *** activemodel (3.0.4.rc) activerecord (3.0.4.rc) activesupport (3.0.4.rc)
I can't test with the master branch, I've this error: /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0.beta/lib/active_record/relation/calculations.rb:169:in `perform_calculation': undefined method `grep' for # (NoMethodError) from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0.beta/lib/active_record/relation/calculations.rb:152:in `calculate' from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0.beta/lib/active_record/relation/calculations.rb:147:in `calculate' from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0.beta/lib/active_record/relation/calculations.rb:58:in `count' from /usr/local/lib/ruby/gems/1.9.1/gems/activerecord-3.1.0.beta/lib/active_record/base.rb:430:in `count' Thanks for your feedback. -
Ludovic Gasc
Alex, I've just tested with your patch of November 13th, 2010 @ 10:52 PM, it runs correctly.
Only your latest patch has a problem.
If I can help, don't hesitate to contact me.
Yours.
-
Alex
The usage should be pp SavedForm.count(:group => [ 'agent_id', 'code_dispo']). The reason we do this is to avoid SQL parsing. This is not documented anywhere yet, so eventually we should be adding this to the rails doc.
-
Ben Woosley
I can confirm this isn't fixed. And +1 for getting it fixed.
-
Ben Woosley
Also, I'll be taking another look at it this weekend.
-
Ludovic Gasc
I confirm this bug isn't fixed in 3.0.7.
I don't understand why the patch isn't applied on the source code since 6 months, what's the problem ?
