Ruby on Rails: updating a boolean record in a database

if my database is set to false, is it best to update the boolean value in the database?

I can do this on the console:

>> u = User.find_by_id(1) 

What should I do next?

thanks

+6
source share
2 answers

If you want to switch the boolean value:

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

If you want to set a boolean:

 u.<attribute> = [true|false] 

If you want to immediately update the boolean value:

 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 
+10
source
 >> u.boolean_property = false >> u.save 

where boolean_property is the name of the property you want to set to false.

This is the easiest way (just install it directly), and there are other ways, depending on your needs: http://www.davidverhasselt.com/2011/06/28/5-ways-to-set-attributes-in- activerecord /

0
source

Source: https://habr.com/ru/post/953842/


All Articles