我注意到Rails(4.1)ActiveRecord有些奇怪,其中select
和count
有时混合不好:
User.all.count
=> 103
User.all.size
=> 103
User.all.length
=> 103
到目前为止,这么好。我可以选择id
:
User.select(:id).all.count
=> 103
User.select(:id).all.size
=> 103
User.select(:id).all.length
=> 103
仍然很好。我也可以选择email
:
User.select(:email).all.count
=> 103
User.select(:email).all.size
=> 103
User.select(:email).all.length
=> 103
但现在问题开始了。当我选择 id
和email
:
User.select(:id, :email).all.count
(0.4ms) SELECT COUNT(id, email) FROM `users`
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' email) FROM `users`' at line 1: SELECT COUNT(id, email) FROM `users`
ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' email) FROM `users`' at line 1: SELECT COUNT(id, email) FROM `users`
User.select(:id, :email).all.size
(0.4ms) SELECT COUNT(id, email) FROM `users`
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' email) FROM `users`' at line 1: SELECT COUNT(id, email) FROM `users`
ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' email) FROM `users`' at line 1: SELECT COUNT(id, email) FROM `users`
User.select(:id, :email).all.length
=> 103
为什么count
和size
(在这种情况下为count
的别名)抛出异常,并且只有当我选择多个属性?
对此有解释吗?我觉得这很意外。
答案 0 :(得分:9)
这是Rails 4.1中的一个错误。见https://github.com/rails/rails/issues/13648
答案 1 :(得分:1)
答案 2 :(得分:0)
感谢@camomileCase给您的提示。
对于我来说,我已经解决了此问题,方法是添加带有count
选项的:all
,并且有时根据需要添加另外的count
。这种方式最适合我:
@grid = myscope.select(:thickness, :width, :length, "SUM(quantity_available) as quantity_available").group(:thickness, :width, :length)
@grid.assets.limit(nil).count(:all)
@grid.assets.limit(nil).count(:all).count