2016-08-23 13 views
2

私は、2つのモデルの間に連想テーブルを設定しようとしています。私のような私のモデルの関連付けを定義していますhas_one&has_many関連テーブルのActiveRecord Association

class Homebase < ApplicationRecord 
    has_many :homebase_addresses 
    has_many :addresses, through: :homebase_address 
end 

class Address < ApplicationRecord 
    has_one :homebase_address 
    has_one :homebase, through: :homebase_address 
end 

そして、私の協会:

class HomebaseAddress < ApplicationRecord 
    belongs_to :homebase 
    belongs_to :address 
end 

は、私の場合は、[OK]を作成:

homebase = Homebase.create 
address = Address.create 
homebase_address = HomebaseAddress.create(homebase: homebase, address: address) 

しかし、

homebase.addresses 

ができます次のエラー:

ActiveRecord::HasManyThroughAssociationNotFoundError: 
     Could not find the association :homebase_address in model Homebase 

ここでは何が欠けていますか?ありがとう!

答えて

3

ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :homebase_address in model Homebase

あなたの問題は本拠地モデルでは、あなたの団体です。あなたはそれを修正​​代わりのhomebase_addresses

class Homebase < ApplicationRecord 
    has_many :homebase_addresses 
    has_many :addresses, through: :homebase_addresses 
              #^^^^^ 
end 
+1

感謝を持っています! –

関連する問題