aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Duncan <mrduncan@gmail.com>2009-08-08 20:10:01 -0400
committerMichael Koziarski <michael@koziarski.com>2009-08-09 12:43:07 +1200
commit3b3798506b403911665c3c24fd055b75d6f6a44f (patch)
tree37de7c99f0efbb990cc2f3b62f81c05921ad592b
parent1f6afe4a74bb815a33f41b2d75acd530de6e2eba (diff)
downloadrails-3b3798506b403911665c3c24fd055b75d6f6a44f.tar.gz
rails-3b3798506b403911665c3c24fd055b75d6f6a44f.tar.bz2
rails-3b3798506b403911665c3c24fd055b75d6f6a44f.zip
Adding :from scoping to ActiveRecord calculations
Signed-off-by: Michael Koziarski <michael@koziarski.com> [#1229 state:committed]
-rw-r--r--activerecord/lib/active_record/calculations.rb2
-rw-r--r--activerecord/test/cases/calculations_test.rb6
-rw-r--r--activerecord/test/models/organization.rb2
3 files changed, 10 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 727f4c1dc6..4a88c43dff 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -197,6 +197,8 @@ module ActiveRecord
sql << ", #{options[:group_field]} AS #{options[:group_alias]}" if options[:group]
if options[:from]
sql << " FROM #{options[:from]} "
+ elsif scope && scope[:from]
+ sql << " FROM #{scope[:from]} "
else
sql << " FROM (SELECT #{distinct}#{column_name}" if use_workaround
sql << " FROM #{connection.quote_table_name(table_name)} "
diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb
index 24bc4f71ce..c2e02763f6 100644
--- a/activerecord/test/cases/calculations_test.rb
+++ b/activerecord/test/cases/calculations_test.rb
@@ -2,6 +2,8 @@ require "cases/helper"
require 'models/company'
require 'models/topic'
require 'models/edge'
+require 'models/club'
+require 'models/organization'
Company.has_many :accounts
@@ -223,6 +225,10 @@ class CalculationsTest < ActiveRecord::TestCase
assert_equal 15, companies(:rails_core).companies.sum(:id)
end
+ def test_should_sum_scoped_field_with_from
+ assert_equal Club.count, Organization.clubs.count
+ end
+
def test_should_sum_scoped_field_with_conditions
assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
end
diff --git a/activerecord/test/models/organization.rb b/activerecord/test/models/organization.rb
index d79d5037c8..c85726169e 100644
--- a/activerecord/test/models/organization.rb
+++ b/activerecord/test/models/organization.rb
@@ -1,4 +1,6 @@
class Organization < ActiveRecord::Base
has_many :member_details
has_many :members, :through => :member_details
+
+ named_scope :clubs, { :from => 'clubs' }
end \ No newline at end of file