Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ActiveRecord::DeleteRestrictionError (MAYBE-RAILS-CA) #1613

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/models/account/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Expand Down
16 changes: 16 additions & 0 deletions test/models/transfer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,20 @@
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)

Check failure on line 104 in test/models/transfer_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
transfer = Transfer.create!(
inflow_transaction: inflow_entry.account_transaction,
outflow_transaction: outflow_entry.account_transaction
)

Check failure on line 109 in test/models/transfer_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
assert_difference 'Transfer.count', -1 do

Check failure on line 110 in test/models/transfer_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
outflow_entry.account_transaction.destroy
end

Check failure on line 113 in test/models/transfer_test.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
assert_nil Transfer.find_by(id: transfer.id)
end
end
Loading