![](/img/trans.png)
[英]Rails ActiveRecord association “has_many, each of which has_one”
[英]ActiveRecord Association for has_one & has_many associative table
我正在嘗試建立兩個模型,它們之間有關聯表。 我已經定義了我的模型關聯:
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
我的實例創建成功:
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
我在這里想念什么? 謝謝堆!
ActiveRecord :: HasManyThroughAssociationNotFoundError:在模型Homebase中找不到關聯:homebase_address
您的問題出在您的Homebase模型中。 您有homebase_address
而不是homebase_addresses
class Homebase < ApplicationRecord
has_many :homebase_addresses
has_many :addresses, through: :homebase_addresses
#^^^^^
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.