Rails中两个类之间的多个属植物关系 [英] multiple belongs_to relationships between two classes in Rails
本文介绍了Rails中两个类之间的多个属植物关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个Transaction类。此类的每个对象都包括一个发行帐户,一个发送帐户和一个接收帐户。这些都是Account类的一个实例。在我的交易表中,我有issuer_id,sender_id和Receiver_id。
I have a Transaction class. Each object of this class includes one issuing account, one sending account and one receiving account. Each of these is an instance of Account class. In my Transaction table, I have issuer_id, sender_id and receiver_id.
我应如何指定交易和帐户之间的关系以便致电
How should I specify relationship between Transaction and Account so that I can call
transaction.issuer
transaction.sender
transaction.receiver
谢谢。
推荐答案
使用:class_name
指定类名(如果可以)不要从关联名称中猜测:
Use :class_name
to specify the class name, when it can't be guessed 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
您可以在文档。
这篇关于Rails中两个类之间的多个属植物关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文