How to delete some associations from an ActiveRecord object without saving

会有一股神秘感。 提交于 2019-12-07 22:36:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!