2017-06-12 58 views
1

我确信这很简单,但我无法让它工作。我有以下关联。理论上,我想查询所有具有名称等于“x”的A记录的D记录。所以像这样:加入伟大的祖父母模型 - Ruby On Rails

@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

你得到什么样的结果? –

+0

@MichaelGorman我已经更新了我的代码中的错误和实际模型名称的问题。我也尝试了大括号。 –

+0

错误消息是说表'category'从未加入到请求中。事实并非如此,因为表格实际上是“类别”。大卫奥尔德里奇的答案应该能解决你的问题 –

回答

2

表名应pluralised - 注意SQL语句文本INNER JOIN "categories"

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