From 087e438eb691b3673b2be00202f12197c89e50fe Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 2 Mar 2022 11:26:53 +0100 Subject: [PATCH] models: delete AdministrateursInstructeur when destroying Instructeur By default, `has_and_belongs_to_many` properly deletes the record in the join table. However, as the association is declared manually with a `has_many / through`, it doesn't delete the joined record automatically. As we also lack a foreign-key contraint on the join table, that means a dangling record remains in the join table. To fix this, let's declare it a proper `has_and_belongs_to_many` association, which will let the join record be deleted automatically on destroy. --- app/models/instructeur.rb | 3 +-- spec/models/instructeur_spec.rb | 4 ++++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/models/instructeur.rb b/app/models/instructeur.rb index 8f72114cd..9c5d07a2b 100644 --- a/app/models/instructeur.rb +++ b/app/models/instructeur.rb @@ -11,8 +11,7 @@ # agent_connect_id :string # class Instructeur < ApplicationRecord - has_many :administrateurs_instructeurs - has_many :administrateurs, through: :administrateurs_instructeurs + has_and_belongs_to_many :administrateurs has_many :assign_to, dependent: :destroy has_many :groupe_instructeurs, through: :assign_to diff --git a/spec/models/instructeur_spec.rb b/spec/models/instructeur_spec.rb index bc0c87d94..b108cfe95 100644 --- a/spec/models/instructeur_spec.rb +++ b/spec/models/instructeur_spec.rb @@ -12,6 +12,10 @@ describe Instructeur, type: :model do procedure_3 end + describe 'associations' do + it { is_expected.to have_and_belong_to_many(:administrateurs) } + end + describe 'follow' do let(:dossier) { create :dossier } let(:already_followed_dossier) { create :dossier }