2016-08-23 7 views
2

Ich versuche, zwei Modelle mit einer assoziativen Tabelle zwischen ihnen einzurichten. Ich habe mein Modell Assoziationen als solche definiert:ActiveRecord Association für has_one & has_many assoziative Tabelle

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 

Und mein Verein:

class HomebaseAddress < ApplicationRecord 
    belongs_to :homebase 
    belongs_to :address 
end 

Meine Instanzen erstellen OK:

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

jedoch

homebase.addresses 

das gibt Folgefehler:

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

Was fehlt mir hier? Tausend Dank!

Antwort

3

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

Ihr Problem ist in Ihren Vereinen in Homebase Modell. Sie haben homebase_address statt homebase_addresses

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

Dank, dass es fest! –

Verwandte Themen