Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Ruby 是否提供增强版的赋值运算符(“ =”),允许您将这两行合并为一条?
=
foo.bar = "woof" foo.save
Ruby没有,但我假设你在 Rails 中这样做,在这种情况下你可以这样做
foo.update_attributes bar: "woof"
ActiveRecord 允许您更新单个属性:
foo.update_attribute :bar, 'woof'
或一次多个属性:
foo.update_attributes bar: 'woof'
请注意,后一种表示法将针对批量分配权限执行安全检查。