vendredi 18 décembre 2015

Rails 4 belong/has_many relation - delete associated attribute but Keep the associated record

I have 2 models: Deal and Prize

Deal

belongs_to :prize,          :foreign_key => 'prize_id'

Prize

has_many   :deals,  dependent: :destroy  

My problem is simple:

If I delete a prize, I want to keep the deals which would be associated but REMOVE the prize_id value inside the Deals objects that were associated with this prize_id that was just deleted.

  • Today, when using dependent:destroy, it deletes the whole line, i.e the whole record associated.

  • I tried removing the 'dependent: :destroy', but then when I delete a prize, it does delete the prize but it lets inside Deal's prized_id column the id number of the dleted prize. which is a problem.

How can I delete a prize and then have the associated records (the deal objects associated) remain / kepe existing and only change inside the Deal's table the prize_id (from the prize_id in question to nil)

Mathieu

Aucun commentaire:

Enregistrer un commentaire