我不認爲這是可能的這些聚合函數,因爲它們返回一個值而不是範圍,請參閱docs。它們都使用的計算方法,正如你可以看到返回一個數字:
# File lib/active_record/calculations.rb, line 117
117: def calculate(operation, column_name, options = {})
118: validate_calculation_options(operation, options)
119: column_name = options[:select] if options[:select]
120: column_name = '*' if column_name == :all
121: column = column_for column_name
122: catch :invalid_query do
123: if options[:group]
124: return execute_grouped_calculation(operation, column_name, column, options)
125: else
126: return execute_simple_calculation(operation, column_name, column, options)
127: end
128: end
129: 0
130: end
如果你想做到這一點,你必須製作手工查詢。
下面是一個例子:
sql = "SELECT max(id) as max_id,
min(id) as min_id
FROM model
GROUP
BY status
"
Model.find_by_sql(sql).each do |row|
puts "min. id: #{row.min_id}, " <<
"max. id: #{row.max_id}, "
end
這對我來說更好 - 很好。 –