Suppose each Project
has_many
Tasks
.
If I do
some_project.tasks = list_of_tasks
some_project.save
The project's tasks get updated even if the save fails. If list_of_tasks
consists of new records, the project's tasks get deleted even if the save fails! WHOA!
If the save fails, the project should have the same tasks it had before I started messing with it. How do I get this behavior and why isn't it the default?
Enclose the statements in a transaction:
Project.transaction do
p.tasks = task_list
p.save!
end
The save!
method throws an exception upon error, which rolls back any changes done to task list.
You can read the documentation if you want to dive a bit more deeply on the subject.
I believe that accepts_nested_attributes_for() will provide the behavior you want:
class Project < ActiveRecord::Base
accepts_nested_attributes_for :tasks
end
This should wrap everything inside a transaction. You then need to build the form that populates the tasks accordingly. The method tasks_attributes
in your Project model is called instead of the tasks
method. See the API for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With