[英]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?如果我的数据库设置为 false,更新数据库中布尔值的最佳做法是什么?
I can do it on console:我可以在控制台上做到:
>> u = User.find_by_id(1)
What do I do next?我接下来该怎么做?
Thanks谢谢
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
>> u.boolean_property = false
>> u.save
where boolean_property
is the name of the property that you want to set to false.其中boolean_property
是要设置为 false 的属性的名称。
That is the simplest way (just setting 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/这是最简单的方法(直接设置),还有其他方法,看你的需要: http : //www.davidverhasselt.com/2011/06/28/5-ways-to-set-attributes-in -活动记录/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.