Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Remove child association from parent

I have this (Contract and Accessory are associated with 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]) 

Now, I'm wanting to remove that specific accessory from @contract. I don't want to delete the record from the DB, but simply want to remove the association between the two.

What's the railsy way of doing this?

Thanks!

like image 545
Wes Foster Avatar asked Aug 19 '12 02:08

Wes Foster


1 Answers

How about this:

@contract.accessories.delete(@accessory) 

See also: How do I remove a single HABTM associated item without deleting the item itself?

like image 164
Chris Salzberg Avatar answered Sep 18 '22 21:09

Chris Salzberg