Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails: Update boolean record in database

if my database is set to as false, whats the best practice in updating boolean value in database?

I can do it on console:

>> u = User.find_by_id(1)

What do I do next?

Thanks

like image 314
hellomello Avatar asked Sep 14 '13 23:09

hellomello


1 Answers

If you want to toggle the boolean:

u.toggle!(:<attribute>)  # Toggles the boolean and saves without validations
u.toggle(:<attribute>)   # Toggles the boolean and does not save

If you want to set the boolean:

u.<attribute> = [true|false]

If you want to update the boolean immediately:

u.update_column(:<attribute>, [true|false])  # Doesn't update timestamps or call callbacks
u.update_attribute(:<attribute>, [true|false])  # Updates timestamps and triggers any callbacks
like image 171
pdobb Avatar answered Oct 13 '22 19:10

pdobb