rails/activerecord/test
Guillaume Malette 976ef40aef Rollback parent transaction when children fails to update (#32796)
* 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]
2018-05-22 15:58:22 -04:00
..
active_record/connection_adapters Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
assets Use YAML to serialize schema cache 2016-11-27 22:09:58 -05:00
cases Rollback parent transaction when children fails to update (#32796) 2018-05-22 15:58:22 -04:00
fixtures Can preload associations through polymorphic associations 2018-04-20 17:28:25 -04:00
migrations Fix occurrences Fixnum|Bignum 2018-03-04 20:44:30 +02:00
models Rollback parent transaction when children fails to update (#32796) 2018-05-22 15:58:22 -04:00
schema Using existing models for building multiple has_one through tests 2018-04-22 04:16:44 +09:00
support Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
config.example.yml Fix tests for prepared_statements: false and queries hitting #select_all 2016-11-19 21:52:52 +05:30
config.rb Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00