From fd394aa077a880cd7341a373d188413d7df61e63 Mon Sep 17 00:00:00 2001 From: "revise-dev[bot]" <181657630+revise-dev[bot]@users.noreply.github.com> Date: Sat, 11 Jan 2025 07:19:53 +0000 Subject: [PATCH] Fix: ActiveRecord::DeleteRestrictionError (MAYBE-RAILS-CA) --- app/models/account/transaction.rb | 4 ++-- test/models/transfer_test.rb | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/models/account/transaction.rb b/app/models/account/transaction.rb index 91fc0420ddf..6e0c576f93c 100644 --- a/app/models/account/transaction.rb +++ b/app/models/account/transaction.rb @@ -6,8 +6,8 @@ class Account::Transaction < ApplicationRecord has_many :taggings, as: :taggable, dependent: :destroy has_many :tags, through: :taggings - has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", dependent: :restrict_with_exception - has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", dependent: :restrict_with_exception + has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", dependent: :destroy + has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", dependent: :destroy accepts_nested_attributes_for :taggings, allow_destroy: true diff --git a/test/models/transfer_test.rb b/test/models/transfer_test.rb index a4460fd2775..27862023915 100644 --- a/test/models/transfer_test.rb +++ b/test/models/transfer_test.rb @@ -97,4 +97,20 @@ class TransferTest < ActiveSupport::TestCase transfer.save! end end + + test "deleting a transaction deletes associated transfer" do + outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500) + inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500) + + transfer = Transfer.create!( + inflow_transaction: inflow_entry.account_transaction, + outflow_transaction: outflow_entry.account_transaction + ) + + assert_difference 'Transfer.count', -1 do + outflow_entry.account_transaction.destroy + end + + assert_nil Transfer.find_by(id: transfer.id) + end end