Rails从父级删除子关联

时间:2012-08-19 02:53:31

标签: ruby-on-rails ruby-on-rails-3 activerecord

我有这个(合同和附件与has_and_belongs_to_many相关):

# Get the contract and specific accessory based on params
@contract  = Contract.find(params[:id])
@accessory = @contract.accessories.find(params[:accessory_id])

现在,我想从@contract中删除该特定配件。我不想从数据库中删除记录,只是想删除两者之间的关联。

这样做有什么困难?

谢谢!

1 个答案:

答案 0 :(得分:48)

这个怎么样:

@contract.accessories.delete(@accessory)

另请参阅:How do I remove a single HABTM associated item without deleting the item itself?