在after_commit钩子中,从事务开始的访问更改



是否有一种方法可以从开始到事务结束时对ActiveRecord应用更改?我尝试了previous_changes,但它只是返回应用于ActiveRecord上的最后一次操作的更改。例如:

Account.transaction do
   account = Account.create(name: 'test')
  account.update(last_name: 'testing')
end

我想知道'name'和'last_name'在事务中是否发生了变化。

您可以检查ActiveModel::Dirty和just:

Account.transaction do
    account = Account.create(name: 'test')
    account.set_attributes(last_name: 'testing')
    account.previous_changes[:last_name].present?
    account.save!
end

最新更新