![](/img/trans.png)
[英]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.