switch_pointを使った際に複数モデルの更新にトランザクションをかけるのに少しハマった。 最初以下のように書いた。
ActiveRecord::Base.transaction do
ModelA.with_writable do
save!
modelB.save!
end
end
ただこれだとrollbackされない。 以下でもダメ。
ModelA.with_writable do
ActiveRecord::Base.transaction do
save!
modelB.save!
end
end
以下のようにするとrollbackされる。
ModelA.transaction_with do save! modelB! end
transaction_withはswitch_pointが定義しているメソッド https://github.com/eagletmt/switch_point/blob/823cce68aab1cfad4e17e3796a1bfd9a35504681/lib/switch_point/model.rb#L71