从has_many中删除对象但不删除Rails中的原始记录? [英] Remove object from has_many but don't delete the original record in Rails?

查看:229
本文介绍了从has_many中删除对象但不删除Rails中的原始记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

Post.paragraphs << new_paragraph

我需要删除段落id = 3,因此下面的记录完全删除: / p>

And I need to remove paragraph by id = 3, so the following deletes the record completely:

Post.paragraphs.find(paragraph_id).destroy
# or
Post.paragraphs.find(paragraph_id).delete

我只需要从has_many关联中删除一个段落。我试图使用 delete destroy 。两种方法都完全从关联的表中删除记录。

I just need to remove a paragraph from has_many association. I tried to use delete and destroy. Both methods completely delete records from the associated tables. How can I just remove them from the "container"?

推荐答案

您不应该使用 delete 对象,但是使用关系的delete方法,

You should not use the delete method on the Paragraphobject, but instead use the delete method of paragraphs relation, like this:

post.paragraphs.delete(Paragraph.find(paragraph_id))

这篇关于从has_many中删除对象但不删除Rails中的原始记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆