Remove object from has_many but don't delete the original record in Rails?

后端 未结 1 1038
难免孤独
难免孤独 2020-12-29 03:21

I have this:

Post.paragraphs << new_paragraph

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

相关标签:
1条回答
  • 2020-12-29 04:16

    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))
    
    0 讨论(0)
提交回复
热议问题