Offer라는 모델이 있습니다.
class Offer < ApplicationRecord
has_many :sections
has_many :offer_items, through: :section
end
class Section < ApplicationRecord
belongs_to :offer
has_many :offer_items
end
class OfferItem < ApplicationRecord
belongs_to :section
belongs_to :offer
end
다음과 같이 데이터베이스를 시드 한 후 :
offer = Offer.create(name: "Offer A")
section = offer.sections.create(name: "Section A")
item = section.offer_items.create(name: "Item A")
항목이 생성되지 않았고 offer_items에 액세스하려는 경우 Offer.first.offer_items
오류가 발생합니다.
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :section in model Offer
또한 OfferItem.attribute_names
모든 속성 을 반환하지만 offer_id
다른 속성 이 작동하지 않는 것처럼 보입니다. 여기서 무슨 일이 일어나고 있습니까?
아니에요
has_many :offer_items, through: :section
이것의
has_many :offer_items, through: :sections
당신은 :section
연관성 이없고 :sections
연관성이 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다