33

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!

1 Answer 1

65

How about this:

@contract.accessories.delete(@accessory)

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

1
  • Yup, that simple! I noticed the question you had referred me to, but the way they explained it was strange. Thanks though!
    – Wes Foster
    Aug 19, 2012 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.