【发布时间】:2012-11-20 09:10:53
【问题描述】:
在扩展ActiveRecord::Base 的特定类中,每当创建这个对象时,我都需要更新一个不同的ActiveRecord 对象。覆盖save 方法并在保存中执行以下操作是否安全?
def save
super
other = self.other
other.name = self.name
other.save!
end
我担心潜在的交易相关问题。我假设这都是 1 笔交易,如果任何部分失败,一切都会回滚?
【问题讨论】:
-
您需要允许哈希参数,因为 update_attribute 使用此方法并传入参数。
def save args = {} super args ...但下面接受的使用回调的答案更安全、更好,因为我不知道这是否涵盖了所有使用 save 的情况。
标签: ruby-on-rails ruby-on-rails-3 activerecord ruby-on-rails-3.2 active-record-query