Multiple belongs to the relationship between two classes in Rails

I have a Transaction class. Each object in this class includes one account, one account, and one account. Each of them is an instance of the Account class. In my transaction table, I have issuer_id, sender_id and receiver_id.

How do I indicate the connection between a transaction and an account so that I can call

transaction.issuer transaction.sender transaction.receiver 

Thanks.

+6
source share
1 answer

Use :class_name to indicate the class name if it cannot be determined from the association name:

 class Transaction belongs_to :issuer, :class_name => 'Account' belongs_to :sender, :class_name => 'Account' belongs_to :receiver, :class_name => 'Account' end class Account has_many :issued_transactions, :foreign_key => :issuer, :class_name => 'Transaction' has_many :sent_transactions, :foreign_key => :sender, :class_name => 'Transaction' has_many :received_transactions, :foreign_key => :receiver, :class_name => 'Transaction' end 

More information can be found at.

+15
source

Source: https://habr.com/ru/post/897282/


All Articles