2016-09-16 5 views
0

は私がオファーhas_manyのアクセスが、このようなデータベースを播種した後

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.first.offer_itemsようoffer_itemsにアクセスしたい場合は、それ

ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :section in model Offer 

また、私はそれを見ることができますOfferItem.attribute_namesすべての属性を返しますが、offer_idはありませんので、他のbelongs_toが機能していないようです。 ここでは何が起こっていますか?

+0

これは前に尋ねられたと思います。この回答を確認してください。 http://stackoverflow.com/a/5120734/2891994 – Samuel

+0

try: 'has_many:offer_items、through::sections'。 'through'値は別の関連付けと一致しなければなりません(エラーはそれをかなりうまく説明します) – Kkulikovskis

答えて

1

それはあなたが:section関連を持たない

has_many :offer_items, through: :sections 

、あなたが:sections関連を持っているのです

has_many :offer_items, through: :section 

ではありません。

+0

どういう愚かな間違いでしたか?だから私はチュートリアルを探して1時間を無駄にしたが、必要なのは手紙 "s"だった。どうもありがとう! – Ancinek

+0

問題ありません!この種のものに執着することについての素晴らしいことは、あなたが次回のために間違いなく覚えていることです。 :) – SteveTurczyn

関連する問題