1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-07 00:58:02 +00:00

Add Retrieve An Object If It Exists as a rails til.

This commit is contained in:
jbranchaud
2015-06-08 08:27:06 -05:00
parent ae2ec1f054
commit a2f47c339d
2 changed files with 20 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
# Retrieve An Object If It Exists
Rails' Active Support provides the `blank?` and `present?` convenience
methods as extensions to many objects. It also extends the Object class by
providing the [`presence`](http://api.rubyonrails.org/classes/Object.html#method-i-presence)
method. This method returns the *receiver* if it is not blank, otherwise
it returns nil.
Instead of doing
```ruby
User.nickname.present? ? User.nickname : User.firstname
```
I can simply do
```ruby
User.nickname.presence || User.firstname
```