mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Calculate sum with SQL, not Enumerable on HasManyThrough Associations. [Dan Peterson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4640 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
d2eafa8b73
commit
ab0277f220
3 changed files with 12 additions and 1 deletions
|
@ -1,5 +1,7 @@
|
|||
*SVN*
|
||||
|
||||
* Calculate sum with SQL, not Enumerable on HasManyThrough Associations. [Dan Peterson]
|
||||
|
||||
* Factor the attribute#{suffix} methods out of method_missing for easier extension. [Jeremy Kemper]
|
||||
|
||||
* Patch sql injection vulnerability when using integer or float columns. [Jamis Buck]
|
||||
|
|
|
@ -48,6 +48,11 @@ module ActiveRecord
|
|||
alias_method method, :<<
|
||||
end
|
||||
|
||||
# Calculate sum using SQL, not Enumerable
|
||||
def sum(*args, &block)
|
||||
calculate(:sum, *args, &block)
|
||||
end
|
||||
|
||||
protected
|
||||
def method_missing(method, *args, &block)
|
||||
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
||||
|
|
|
@ -371,6 +371,10 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
|
|||
assert_raise(ActiveRecord::ReadOnlyAssociation) { posts(:thinking).tags.create(:name => 'foo') }
|
||||
end
|
||||
|
||||
def test_has_many_through_sum_uses_calculations
|
||||
assert_nothing_raised { authors(:david).comments.sum(:post_id) }
|
||||
end
|
||||
|
||||
private
|
||||
# create dynamic Post models to allow different dependency options
|
||||
def find_post_with_dependency(post_id, association, association_name, dependency)
|
||||
|
|
Loading…
Reference in a new issue