[英]Is it possible to update a database record using RJS in Ruby on Rails?
[英]Ruby on Rails: Update boolean record in database
如果我的数据库设置为 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 -活动记录/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.