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
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
如果要切换布尔值:
u.toggle!(:<attribute>) # Toggles the boolean and saves without validations
u.toggle(:<attribute>) # Toggles the boolean and does not save
如果要设置布尔值:
u.<attribute> = [true|false]
如果要立即更新布尔值:
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
>> u.boolean_property = false
>> u.save
其中boolean_property
是要设置为 false 的属性的名称。
这是最简单的方式(直接设置即可),还有其他方式,看你的需要: http ://www.davidverhasselt.com/2011/06/28/5-ways-to-set-attributes-in -活动记录/