aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/calculations.rb3
-rw-r--r--activerecord/test/calculations_test.rb12
3 files changed, 16 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index 2c48239303..e6f923d2c2 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*1.14.1* (April 6th, 2005)
+* Enable Limit/Offset in Calculations (closes #4558) [lmarlow@yahoo.com]
+
* Fixed that loading including associations returns all results if Load IDs For Limited Eager Loading returns none (closes #4528) [Rick]
* Fixed HasManyAssociation#find bugs when :finder_sql is set #4600 [lagroue@free.fr]
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index a4c6b71398..b62d205201 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -1,6 +1,6 @@
module ActiveRecord
module Calculations #:nodoc:
- CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct]
+ CALCULATIONS_OPTIONS = [:conditions, :joins, :order, :select, :group, :having, :distinct, :limit, :offset]
def self.included(base)
base.extend(ClassMethods)
end
@@ -153,6 +153,7 @@ module ActiveRecord
sql << " GROUP BY #{options[:group_field]}" if options[:group]
sql << " HAVING #{options[:having]}" if options[:group] && options[:having]
sql << " ORDER BY #{options[:order]}" if options[:order]
+ add_limit!(sql, options)
sql.join
end
diff --git a/activerecord/test/calculations_test.rb b/activerecord/test/calculations_test.rb
index 0acc841712..fbf830e9e6 100644
--- a/activerecord/test/calculations_test.rb
+++ b/activerecord/test/calculations_test.rb
@@ -48,6 +48,18 @@ class CalculationsTest < Test::Unit::TestCase
assert_equal [6, 2, 1], c.keys.compact
end
+ def test_should_limit_calculation
+ c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
+ :group => :firm_id, :order => "firm_id", :limit => 2)
+ assert_equal [1, 2], c.keys.compact
+ end
+
+ def test_should_limit_calculation_with_offset
+ c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
+ :group => :firm_id, :order => "firm_id", :limit => 2, :offset => 1)
+ assert_equal [2, 6], c.keys.compact
+ end
+
def test_should_group_by_summed_field_having_condition
c = Account.sum(:credit_limit, :group => :firm_id,
:having => 'sum(credit_limit) > 50')