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.
This commit is contained in:
Pierre de La Morinerie 2022-03-02 11:26:53 +01:00
parent 92e79027c4
commit 087e438eb6
2 changed files with 5 additions and 2 deletions

View file

@ -11,8 +11,7 @@
# agent_connect_id :string # agent_connect_id :string
# #
class Instructeur < ApplicationRecord class Instructeur < ApplicationRecord
has_many :administrateurs_instructeurs has_and_belongs_to_many :administrateurs
has_many :administrateurs, through: :administrateurs_instructeurs
has_many :assign_to, dependent: :destroy has_many :assign_to, dependent: :destroy
has_many :groupe_instructeurs, through: :assign_to has_many :groupe_instructeurs, through: :assign_to

View file

@ -12,6 +12,10 @@ describe Instructeur, type: :model do
procedure_3 procedure_3
end end
describe 'associations' do
it { is_expected.to have_and_belong_to_many(:administrateurs) }
end
describe 'follow' do describe 'follow' do
let(:dossier) { create :dossier } let(:dossier) { create :dossier }
let(:already_followed_dossier) { create :dossier } let(:already_followed_dossier) { create :dossier }