Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

ActiveRecord :group option longer than 1 line no longer works in 2.3.11 #1107

Closed
jdwyah opened this Issue May 17, 2011 · 4 comments

Comments

Projects
None yet
3 participants

jdwyah commented May 17, 2011

The following should be valid, but instead blows up:
User.count(:group => "case when id < 100 then '100'
else '200' end")

This commit:
1681ede#activerecord/lib/active_record/calculations.rb
calls Array() on the grouping string which ends up creating two group_fields which it then tries to alias.

The SQL it creates is:

SELECT count(*) AS count_all, case when id < 100 then '100'
AS case_when_id_100_then_100, else '200' end AS else_200_end FROM "users" GROUP BY case when id < 100 then '100', else '200' end

This is a regression from 2.3.5

Contributor

miloops commented May 17, 2011

Hey I can't reproduce this on master, neither on beta1.

Loading development environment (Rails 3.1.0.beta1)
ruby-1.9.2-p180 :001 > p Land.count(:group => "case when id < 2 then '100'
ruby-1.9.2-p180 :002"> else '200' end")
(0.2ms) SELECT COUNT(*) AS count_all, case when id < 2 then '100'
else '200' end AS case_when_id_2_then_100_else_200_end FROM "lands" GROUP BY case when id < 2 then '100'
else '200' end
=> {"100"=>1, "200"=>3}

ruby-1.9.2-p180 :006 > Land.count(:group => "case when id < 2 then '100' \n else '200' end")
(0.3ms) SELECT COUNT(*) AS count_all, case when id < 2 then '100'
else '200' end AS case_when_id_2_then_100_else_200_end FROM "lands" GROUP BY case when id < 2 then '100'
else '200' end
=> {"100"=>1, "200"=>3}

Which version is failing for you? Does your Usel model has any special configuration?

@josevalim josevalim closed this May 17, 2011

jdwyah commented May 17, 2011

By bad, this is against 2.3.11.

jdwyah commented May 17, 2011

Actually, you might check again in Ruby 1.8. It appears that this behavior changes across ruby version.

ree-1.8.7-2011.03 :002 > Array("aa\nbb")
=> ["aa\n", "bb"]
ruby-1.9.2-p180 :008 > Array("aa\nbb")
=> ["aa\nbb"]

good times.

Contributor

miloops commented May 17, 2011

Tried running this agaisnt 1.8.7 and 1.9.2:

Topic.count(:group => "case when id < 2 then '2'
else '3' end")

1.9.2 output: {"2"=>1, "3"=>3}

1.8.7 output: <OrderedHash {"2"=>1, "3"=>3}>

Same results when running: Topic.count(:group => "case when id < 2 then '2' \n else '3' end")

All this was tested against master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment