问题
So I have:
@project = Project.last
@project.tasks = @project.tasks.reject{|o| o.stupid? }
# Do more stuff
When I update the tasks array, it actually saves it which we don't want.
How do we prevent that?
回答1:
The save happens when you reassign the result to @project.tasks.
Just don't reassign it, and use reject! to change the original @project.tasks:
@project = Project.last
@project.tasks.reject!{ |o| o.stupid? }
来源:https://stackoverflow.com/questions/22927284/how-to-delete-some-associations-from-an-activerecord-object-without-saving