Fix crash deleting admin with assigned budgets

Add missing relation between administrator and budget_administrators, otherwise
we'd get the following exception when deleting and administrator with assigned budgets:

PG::ForeignKeyViolation:
ERROR:  update or delete on table "administrators" violates foreign key constraint "fk_rails_ee7dc33688" on table "budget_administrators"
DETAIL:  Key (id)=(3) is still referenced from table "budget_administrators".
This commit is contained in:
Senén Rodero Rodríguez
2022-05-03 16:07:50 +02:00
parent 0d70b76331
commit d797ec3ca0
2 changed files with 11 additions and 0 deletions

View File

@@ -1,5 +1,7 @@
class Administrator < ApplicationRecord class Administrator < ApplicationRecord
belongs_to :user, touch: true belongs_to :user, touch: true
has_many :budget_administrators, dependent: :destroy
delegate :name, :email, :name_and_email, to: :user delegate :name, :email, :name_and_email, to: :user
validates :user_id, presence: true, uniqueness: true validates :user_id, presence: true, uniqueness: true

View File

@@ -38,4 +38,13 @@ describe Administrator do
expect(administrator.description_or_name_and_email).to eq("Billy Wilder (test@test.com)") expect(administrator.description_or_name_and_email).to eq("Billy Wilder (test@test.com)")
end end
end end
describe "#destroy" do
it "removes dependent budget administrator records" do
administrator = create(:administrator)
create_list(:budget, 2, administrators: [administrator])
expect { administrator.destroy }.to change { BudgetAdministrator.count }.by(-2)
end
end
end end