10

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

4

2 回答 2

14

如果要切换布尔值:

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
于 2013-09-14T23:50:22.483 回答
0
>> u.boolean_property = false
>> u.save

其中boolean_property是要设置为 false 的属性的名称。

这是最简单的方式(直接设置即可),还有其他方式,看你的需要: http ://www.davidverhasselt.com/2011/06/28/5-ways-to-set-attributes-in -活动记录/

于 2013-09-14T23:08:15.317 回答