如果我的数据库设置为false,那么在更新布尔值时最佳实践是什么?
我可以在控制台上执行操作:
>> u = User.find_by_id(1)
我接下来该做什么?
谢谢
如果我的数据库设置为false,那么在更新布尔值时最佳实践是什么?
我可以在控制台上执行操作:
>> u = User.find_by_id(1)
我接下来该做什么?
谢谢
如果您想切换布尔值:
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-activerecord/
heroku run console
中,它说undefined method 'boolean_property。另外,我正在尝试将其更新为true
。 - hellomello