Files
nairobi/app/models/administrator.rb
Senén Rodero Rodríguez d797ec3ca0 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".
2022-05-03 18:39:42 +02:00

19 lines
434 B
Ruby

class Administrator < ApplicationRecord
belongs_to :user, touch: true
has_many :budget_administrators, dependent: :destroy
delegate :name, :email, :name_and_email, to: :user
validates :user_id, presence: true, uniqueness: true
scope :with_user, -> { includes(:user) }
def description_or_name
description.presence || name
end
def description_or_name_and_email
"#{description_or_name} (#{email})"
end
end