Skip to content

Commit

Permalink
Add example for AR::Persistence#toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
radar committed Nov 21, 2015
1 parent f96b19b commit 7461875
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions activerecord/lib/active_record/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ def decrement!(attribute, by = 1)
# if the predicate returns +true+ the attribute will become +false+. This
# method toggles directly the underlying value without calling any setter.
# Returns +self+.
#
# Example:
#
# user = User.first
# user.banned? # => false
# user.toggle(:banned)
# user.banned? # => true
#
def toggle(attribute)
self[attribute] = !public_send("#{attribute}?")
self
Expand Down

0 comments on commit 7461875

Please sign in to comment.