2017-06-12 6 views
1

これは非常に簡単だと確信していますが、動作させることはできません。私は以下の団体を持っています。ジョーンズグレート・パシフィック・モデル - Ruby On Rails

model Category 
    has_many :category_brands 
end 

model CategoryBrand 
    has_many :category_models 
    belongs_to :category 
end 

model CategoryModel 
    has_many :products 
    belongs_to :category_brand 
end 

model Product 
    belongs_to :category_model 
end 

理論上、「x」に等しい名前のAレコードを持つすべてのDレコードを照会したいと考えています。だからこのように:

@products = Product.joins(category_model: {category_brand: :category}).where("category.name like ?", "%Incline Motors%") 

しかし、私はこれを動作させることはできません。どんな助けもありがとう。

現在のエラー:

G::UndefinedTable: ERROR: missing FROM-clause entry for table "category" LINE 1: ...es"."id" = "category_brands"."category_id" WHERE (category.n...^: SELECT COUNT(*) FROM "products" INNER JOIN "category_models" ON "category_models"."id" = "products"."category_model_id" INNER JOIN "category_brands" ON "category_brands"."id" = "category_models"."category_brand_id" INNER JOIN "categories" ON "categories"."id" = "category_brands"."category_id" WHERE (category.name like '%Incline Motors%') 
+0

何の結果を得ているSQL文のテキストに注意してください? –

+0

@MichaelGorman私は自分の質問でエラーと実際のモデル名を更新しました。私は中括弧も試しました。 –

+0

エラーメッセージは、テーブル 'category'がリクエストに結合されていないことを示しています。テーブルは実際には「カテゴリ」なのではありません。 David Aldridgeの答えがあなたの問題を解決するはずです –

答えて

2

テーブル名はpluralisedする必要があります - あなたはINNER JOIN "categories"

@products = Product.joins(category_model: {category_brand: :category}).where("categories.name like ?", "%Incline Motors%")