aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-05-24 20:50:44 -0300
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2012-05-28 23:16:34 -0300
commitf9cb645dfcb5cc89f59d2f8b58a019486c828c73 (patch)
treefe959ec9be9888c650975a75e6e5e185fd261290
parent9cc2bf69ce296b7351dc612a8366193390a305f3 (diff)
downloadrails-f9cb645dfcb5cc89f59d2f8b58a019486c828c73.tar.gz
rails-f9cb645dfcb5cc89f59d2f8b58a019486c828c73.tar.bz2
rails-f9cb645dfcb5cc89f59d2f8b58a019486c828c73.zip
Yield only one argument instead of splatting.
Add Changelog entry. Closes #4003
-rw-r--r--activerecord/CHANGELOG.md7
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb4
2 files changed, 9 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 1a8e233a51..4f4e087acd 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,12 @@
## Rails 4.0.0 (unreleased) ##
+* Allow blocks for `count` with `ActiveRecord::Relation`, to work similar as
+ `Array#count`:
+
+ Person.where("age > 26").count { |person| gender == 'female' }
+
+ *Chris Finne & Carlos Antonio da Silva*
+
* Added support to `CollectionAssociation#delete` for passing `fixnum`
or `string` values as record ids. This finds the records responding
to the `id` and executes delete on them.
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index a0d24f28d0..04b4fcf379 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -21,7 +21,7 @@ module ActiveRecord
# # => queries people where "age > 26" then count the loaded results filtering by gender
def count(column_name = nil, options = {})
if block_given?
- self.to_a.count { |*block_args| yield(*block_args) }
+ self.to_a.count { |item| yield item }
else
column_name, options = nil, column_name if column_name.is_a?(Hash)
calculate(:count, column_name, options)
@@ -65,7 +65,7 @@ module ActiveRecord
# # queries people where "age > 100" then perform a sum calculation with the block returns
def sum(*args)
if block_given?
- self.to_a.sum(*args) { |*block_args| yield(*block_args) }
+ self.to_a.sum(*args) { |item| yield item }
else
calculate(:sum, *args)
end