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:
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user