Fix crash destroying budgets with admins/valuators

We don't allow deleting a budget with associated investments. However,
we allow deleting a budget with associated administrators and valuators.
This results in a foreign key violation error:

PG::ForeignKeyViolation: ERROR:  update or delete on table "budgets"
violates foreign key constraint "fk_rails_c847a52b1d" on table
"budget_administrators"

Using the `dependent: :destroy` option when defining the relationship,
we remove the association records when removing the budget.

As a bonus, we reduce the number of Rubocop offenses regarding the
`Rails/HasManyOrHasOneDependent` rule. Only 72 to go! :)
This commit is contained in:
Javi Martín
2021-05-23 23:57:28 +02:00
parent 99361a958a
commit a0942f66bf
3 changed files with 36 additions and 2 deletions

View File

@@ -34,9 +34,9 @@ class Budget < ApplicationRecord
has_many :headings, through: :groups
has_many :lines, through: :ballots, class_name: "Budget::Ballot::Line"
has_many :phases, class_name: "Budget::Phase"
has_many :budget_administrators
has_many :budget_administrators, dependent: :destroy
has_many :administrators, through: :budget_administrators
has_many :budget_valuators
has_many :budget_valuators, dependent: :destroy
has_many :valuators, through: :budget_valuators
has_one :poll