提交 3b379850 编写于 作者: M Matt Duncan 提交者: Michael Koziarski

Adding :from scoping to ActiveRecord calculations

Signed-off-by: NMichael Koziarski <michael@koziarski.com>
[#1229 state:committed]
上级 1f6afe4a
......@@ -197,6 +197,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)} "
......
......@@ -2,6 +2,8 @@
require 'models/company'
require 'models/topic'
require 'models/edge'
require 'models/club'
require 'models/organization'
Company.has_many :accounts
......@@ -223,6 +225,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
......
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册