Let's use any model (i.e. Model) and execute in console:
Model.select('id').group('id').having('2 >
1')
and we'll get the sql query similar to:
SELECT id FROM "models" GROUP BY id HAVING 2 >
1
That is okay.
Then execute in console:
Model.select('id').group('id').having('2 >
1').having('3 > 1')
and we'll get the following error:
ArgumentError: wrong number of arguments (2 for 1)
from /home/cryo/.rvm/gems/ruby-1.8.7-p334@appstore/gems/activerecord-3.0.5/lib/active_record/relation/query_methods.rb:181:in `having'
from /home/cryo/.rvm/gems/ruby-1.8.7-p334@appstore/gems/activerecord-3.0.5/lib/active_record/relation/query_methods.rb:181:in `build_arel'
from /home/cryo/.rvm/gems/ruby-1.8.7-p334@appstore/gems/activerecord-3.0.5/lib/active_record/relation/query_methods.rb:150:in `arel'
from /home/cryo/.rvm/gems/ruby-1.8.7-p334@appstore/gems/activerecord-3.0.5/lib/active_record/relation.rb:64:in `to_a'
from /home/cryo/.rvm/gems/ruby-1.8.7-p334@appstore/gems/activerecord-3.0.5/lib/active_record/relation.rb:356:in `inspect'
Why does it happen?
Calling having on ActiveRecord::Relation collects having clauses
into an array
relation.having_values += build_where(*args) unless
args.blank?
The the following code in
active_record/relation/query_methods.rb:181:in `build_arel' passes
as many arguments to arel.having as the quantity of collection
having clauses in relation. But in seems the having method of arel
accepts only 1 argument expr.
arel = arel.having(*@having_values.uniq.reject{|h|
h.blank?}) unless @having_values.empty?