Rails: HasManyThroughAssociationNotFoundError

后端 未结 3 1851
面向向阳花
面向向阳花 2020-12-23 16:36

I have problems with getting a has_many through association to work.

I keep getting this exception:

Article.find(1).warehouses.build
Act         


        
相关标签:
3条回答
  • 2020-12-23 16:51

    You would need to add

    has_many :entries
    

    To each model, and above has_many :through, like this:

    class Article < ActiveRecord::Base
      has_many :entries
      has_many :warehouses, :through => :entries
    end
    
    class Warehouse < ActiveRecord::Base
      has_many :entries
      has_many :articles, :through => :entries
    end
    

    More detailed tutorial on how to handle view and controllers https://kolosek.com/rails-join-table/

    0 讨论(0)
  • 2020-12-23 16:52

    You need to add

    has_many :entries
    

    To each of your models, since the :through option just specifies a second association which it should use to find the other side.

    0 讨论(0)
  • 2020-12-23 17:10

    @Meekohi This means that you have no Entry model. I just received the error message myself, so wanted to point it out (can't post it as a comment due to low reputation).

    class Entry < ActiveRecord::Base
      belongs_to :article
      belongs_to :warehouse
    end
    

    Simply run

    rails g model Entry
    
    0 讨论(0)
提交回复
热议问题