Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails difference between Model.count and Model.count(:all)

Is there any difference between

User.count 

and

User.count(:all)

I upgraded rails to 4.0 then when I use ModelName.count(:all) it's working well but if I use ModelName.count the following error occurs.By the way bot of them is working well in rails 3.2

SELECT COUNT() FROM "users"
PG::WrongObjectType: ERROR:  count(*) must be used to call a parameterless aggregate function
LINE 1: SELECT COUNT() FROM "users"
like image 770
Johnny Cash Avatar asked Aug 15 '13 12:08

Johnny Cash


1 Answers

I ran into this issue as well. The change was introduced in this commit. A line like

User.count

will now throw an ActiveRecord::StatementInvalid error since it will generate SELECT COUNT() FROM users on Postgres. As of this commit, the fix is to update your code to

User.count(:all)

This commit restores the functionality that existed previously, using :all as the "column" to pass on to ARel, causing a valid SQL query SELECT COUNT(*) FROM users.

My Gemfile originally had the following (as mentioned in the comments)

gem "rails", github: "rails/rails", branch: "4-0-stable"

but I needed to run bundle update rails to pull down the newer commit referenced above.

like image 195
deefour Avatar answered Sep 28 '22 20:09

deefour