• G
    Rollback parent transaction when children fails to update (#32796) · 976ef40a
    Guillaume Malette 提交于
    * Rollback parent transaction when children fails to update
    
    Rails supports autosave associations on the owner of a `has_many`
    relationship. In certain situation, if the children of the association
    fail to save, the parent is not rolled back.
    
    ```ruby
    class Employee < ActiveRecord::Base
    end
    
    class Company < ActiveRecord::Base
      has_many(:employees)
    end
    
    company = Company.new
    employee = company.employees.new
    company.save
    ```
    
    In the previous example, if the Employee failed to save, the Company
    will not be rolled back. It will remain in the database with no
    associated Employee.
    
    I expect the `company.save` call to be atomic, and either create all or
    none of the records.
    
    The persistance of the Company already starts a transaction that nests
    it's children. However, it didn't track the success or failure of it's
    children in this very situation, and the outermost transaction is not
    rolled back.
    
    This PR makes the change to track the success of the child insertion and
    rollback the parent if any of the children fail.
    
    * Change the test to reflect what we expect
    
    Once #32862 is merged, rolling back a record will rollback it's state to match
    the state before the database changes were applied
    
    * Use only the public API to express the tests
    
    * Refactor to avoid reassigning saved for nested reflections
    
    [Guillaume Malette + Rafael Mendonça França]
    976ef40a
autosave_association.rb 18.9 KB