Method: ActiveRecord::Persistence#toggle
- Defined in:
- activerecord/lib/active_record/persistence.rb
#toggle(attribute) ⇒ Object
Assigns to attribute the boolean opposite of attribute?. So 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
683 684 685 686 |
# File 'activerecord/lib/active_record/persistence.rb', line 683 def toggle(attribute) self[attribute] = !public_send("#{attribute}?") self end |