I'm working with transactions that can be either purchases or refunds and I need to connect them to each other. One purchase can have several refunds (partial refunds), and one refund can be connected to several purchases.
I tried to establish relationships as described here
Here is my Transaction model:
class Transaction < ApplicationRecord
has_many :refunds_purchases, foreign_key: :transaction_id, class_name: 'RefundsPurchase'
has_many :refunds, through: :purchases_refunds, source: :refund
has_many :purchases_refunds, foreign_key: :transaction_id, class_name: 'RefundsPurchase'
has_many :purchases, through: :refunds_purchases, source: :purchase
end
And here is the association model:
class RefundsPurchase < ApplicationRecord
belongs_to :purchase, foreign_key: :purchase_id, class_name: 'Transaction'
belongs_to :refund, foreign_key: :refund_id, class_name: 'Transaction'
end
When I call transaction.refunds it fails with NameError: uninitialized constant Transaction::RefundsPurchase"
so it's trying to prefix the namespace with the current class prefix. If I move the associative model to the Transaction::RefundsPurchase
namespace, it gives NoMethodError: undefined method
refunds_purchases' for #Transaction:0x000055633d746440`
What am I doing wrong?