Permalink
Browse files

Adding :from scoping to ActiveRecord calculations

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
1 parent 389449d commit 407fbb50907b34f062c7670b570c44abeb21e2f6 @mrduncan mrduncan committed with NZKoz Aug 8, 2009
@@ -190,6 +190,8 @@ def construct_calculation_sql(operation, column_name, options) #:nodoc:
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)} "
@@ -5,6 +5,8 @@
require 'models/owner'
require 'models/pet'
require 'models/toy'
+require 'models/club'
+require 'models/organization'
Company.has_many :accounts
@@ -226,6 +228,10 @@ def test_should_sum_scoped_field
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
@@ -1,4 +1,6 @@
class Organization < ActiveRecord::Base
has_many :member_details
has_many :members, :through => :member_details
+
+ named_scope :clubs, { :from => 'clubs' }
end

0 comments on commit 407fbb5

Please sign in to comment.