2017-05-15 59 views
0

我發現在rails的destroy方法中,一個額外的事務會包裝在查詢上。因此,當我嘗試以下它不起作用。在同一個事務中同時執行刪除和插入記錄操作?

ActiveRecord::Base.transaction do 
@model_a.destroy_all 
@new_model_list.each do |item| 
    item.create! 
    # Error occurs some point 
    # No rollback for those deleted records 
end 
end 
+1

正確的方法使用ActiveRecord事務是要麼使用'的ActiveRecord :: Base.transaction'或'SomeModel.transaction'。我不知道你的代碼如何運行destroy_all行。 – TheGeorgeous

+0

我的錯。我錯過了::基地部分。謝謝。 –

回答

0

嘗試重寫這:

ActiveRecord::Base.connection.transaction do 
@model_a.destroy_all 
@new_model_list.each do |item| 
    item.create! 
    # Error occurs some point 
    # No rollback for those deleted records 
end 
end 
相關問題