开发者

Multiple 'having' clauses blows up Rails 3 / Arel

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/ge开发者_运维问答ms/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.


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])


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜