In Ruby on Rails, for an ActiveRecord class MySettings
, is there a more elegant option to say
s = MySettings.last
s.do_updates = true
s.save
in one line?
MySettings.last.update_attributes :do_updates => true
MySettings.last.update_attributes(:do_updates => true)
Although I typically would recommend update_attributes
as the others have suggested, this is the perfect situation to use update_column
which is replacing update_attribute
. update_attribute
is being removed in rails 4.x and will issue deprecation warnings in later 3.2.x releases.
MySettings.last.update_column(:do_updates, true)
Unlike update_attributes
, update_column
does not execute validations or callbacks. If you need the ActiveRecord callbacks, definitely use update_attrbutes
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.