我确信这很简单,但我无法让它发挥作用。我有以下协会。
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 :(得分:2)
表名应该是复数 - 请注意SQL语句文本INNER JOIN "categories"
@products = Product.joins(category_model: {category_brand: :category}).where("categories.name like ?", "%Incline Motors%")