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:
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.