Does anyone have a valid use case for
ActiveRecord::Base#toggle! - inquiring
minds want to know!
The implementation from the rails codebase…
def toggle!(attribute) toggle(attribute).update_attribute(attribute, self[attribute]) end
So you send an
ActiveRecord instance an attribute whose state you want to flip
around, it flips it around, then it saves the instance with
which means no validations will be run.
Here’s an example…
>> user = User.find :first => #<User id: 1, first_name: nil, last_name: nil, sysadmin: false, ...> >> user.sysadmin? => false >> user.toggle! :sysadmin => true >> user.reload.sysadmin? => true
…the problem is that while it’s easy to come up with ways that you CAN use this feature, we can’t come up with a use case where you SHOULD use this feature. Please enlighten us - but be warned that your reply will be evaluated within the context of various best practice rules that you may or may not be aware of.