aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2014-08-29 15:04:37 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2014-08-29 15:04:37 -0700
commitcc0d8fbec96aeb1664fe98d45929a236b18aec81 (patch)
tree6a78eee1cd404f73a0a74a7cbc52332fb400f4f8
parent141d864e0ec1098176141e6cb6aef3eaf07e830f (diff)
downloadrails-cc0d8fbec96aeb1664fe98d45929a236b18aec81.tar.gz
rails-cc0d8fbec96aeb1664fe98d45929a236b18aec81.tar.bz2
rails-cc0d8fbec96aeb1664fe98d45929a236b18aec81.zip
Update examples to show real worth
-rw-r--r--activesupport/CHANGELOG.md11
-rw-r--r--activesupport/lib/active_support/core_ext/object/blank.rb2
2 files changed, 11 insertions, 2 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 6830360eba..b32c97bead 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,6 +1,15 @@
* Added yield to Object#presence, so you can do this:
- person.presence { |p| p.name.first } || 'Nobody'
+ project.account.owner.presence { |p| p.name.first } || 'Nobody'
+
+ instead of calling twice (which may incur double SQL calls):
+
+ project.account.owner ? project.account.owner.name.first || 'Nobody'
+
+ or assigning to local variable:
+
+ owner = project.account.owner
+ owner ? owner.name.first || 'Nobody'
*DHH*
diff --git a/activesupport/lib/active_support/core_ext/object/blank.rb b/activesupport/lib/active_support/core_ext/object/blank.rb
index 164a3c47d0..893858fcd6 100644
--- a/activesupport/lib/active_support/core_ext/object/blank.rb
+++ b/activesupport/lib/active_support/core_ext/object/blank.rb
@@ -42,7 +42,7 @@ class Object
# You can also use this with a block that will be yielded if the object is present
# and the result of that block will then be returned
#
- # person.presence { |p| p.name.first } || 'Nobody'
+ # project.account.owner.presence { |p| p.name.first } || 'Nobody'
#
# @return [Object]
def presence