multiple belongs_to relationships between two classes in Rails

前端 未结 1 1099
不知归路
不知归路 2021-01-02 23:02

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 cl

相关标签:
1条回答
  • 2021-01-02 23:27

    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
    

    You can read more in the documentation.

    0 讨论(0)
提交回复
热议问题