多重“拥有”条款炸毁了 Rails 3 / Arel
我想我在 Rails 3 / Arel 中发现了一个错误,但我希望有人可以在我尝试修复它和/或提交错误报告之前澄清我的问题。
- 在一个非常简单的应用程序上,带有一个问题模型:(submitter_id:integer,option_count:integer)
我正在使用的代码:
q = Question.where(:submitter_id => 1)
q = q.having(['option_sum > ?', 5])
q = q.having(['option_sum < ?', 10])
q = q.select("#{Question.table_name}.*, MAX(#{Question.table_name}.option_count) AS option_sum")
q.to_sql
q
这会爆炸:
ArgumentError: wrong number of arguments (2 for 1)
from ~/.rvm/gems/ruby-1.9.2-p0/bundler/gems/arel-f092ae544f58/lib/arel/select_manager.rb:94:in `having'
from ~/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails-76053fe4d12b/activerecord/lib/active_record/relation/query_methods.rb:193:in `build_arel'
from ~/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails-76053fe4d12b/activerecord/lib/active_record/relation/query_methods.rb:162:in `arel'
取出“having”子句之一可以修复问题,并生成正确的SQL。
任何意见将不胜感激。 Arel 和 Rails 3 是边缘版本,其修订如上面的错误中所列。
I think I've found a bug in Rails 3 / Arel, but I'm hoping someone can clarify my problem before I try and fix it and/or submit a bug report.
- On a very simple app, with a Question model: (submitter_id: integer, option_count: integer)
The code that I'm using:
q = Question.where(:submitter_id => 1)
q = q.having(['option_sum > ?', 5])
q = q.having(['option_sum < ?', 10])
q = q.select("#{Question.table_name}.*, MAX(#{Question.table_name}.option_count) AS option_sum")
q.to_sql
q
This blows up with:
ArgumentError: wrong number of arguments (2 for 1)
from ~/.rvm/gems/ruby-1.9.2-p0/bundler/gems/arel-f092ae544f58/lib/arel/select_manager.rb:94:in `having'
from ~/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails-76053fe4d12b/activerecord/lib/active_record/relation/query_methods.rb:193:in `build_arel'
from ~/.rvm/gems/ruby-1.9.2-p0/bundler/gems/rails-76053fe4d12b/activerecord/lib/active_record/relation/query_methods.rb:162:in `arel'
Taking out one of the 'having' clauses fixes the issue, and generates the proper SQL.
Any comments would be appreciated. Arel and Rails 3 are edge versions, with revisions as listed in the error above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有时,当作用域运算符不是类似 SQL 的顺序时,Arel 会给出类似的错误,例如尝试在havings 之前调用 select("..") 。另外,我不确定这样的请求是否允许多种拥有,所以尝试
q = q.having(['(option_sum > ? AND option_sum < ?)', 5, 10])
Sometimes Arel gives similar error, when scope operators are not in SQL-like order, for example try to invoke select("..") before havings. Also, I'm not sure, that multiple havings are allowed in such requests, so try
q = q.having(['(option_sum > ? AND option_sum < ?)', 5, 10])
经过广泛的测试,这看起来像是一个 Rails 错误,尽管我还无法确认它。这个问题也发生在最小的测试 Rails 应用程序上。
After extensive testing, this looks like a Rails bug after all, although I haven't been able to confirm it. This problem occurs on a minimal test Rails app as well.