Rails: ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s)

前端 未结 2 1509
长情又很酷
长情又很酷 2021-01-01 22:52

I have the following code (somewhat simplified ...

create_table :signatures do |t|
  t.integer :signer_id
  t.integer :card_id

  t.timestamps
end

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 23:45

    User should be defined like this:

    class User < ActiveRecord::Base
    
      has_many :sent_cards, :class_name => "Card", :foreign_key => "sender_id"
      has_many :received_cards, :class_name => "Card", :foreign_key => "recipient_id"
    
      has_many :signatures
      has_many :signed_cards, :through => :signatures, :source => :card
    
    end
    

    When your association name is different than the name used at the :through you have to define the source parameter. If you look at the exception message it explicitly asks you to do it.

提交回复
热议问题