我有以下查询
def all_categories
Category.includes(:categorizations).joins("INNER JOIN galleries ON categorizations.categorizeable_type = 'Gallery' AND categorizations.categorizeable_id = galleries.id").where("galleries.galleriable_id = :brand_id AND galleries.galleriable_type = 'Brand'", brand_id: brand_id).distinct
end
导致SQL
SELECT DISTINCT "categories".* FROM "categories" INNER JOIN galleries ON categorizations.categorizeable_type = 'Gallery' AND categorizations.categorizeable_id = galleries.id WHERE (galleries.galleriable_id = 1093 AND galleries.galleriable_type = 'Brand')
不起作用,并给出错误
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "categorizations"
LINE 1: ...ries".* FROM "categories" INNER JOIN galleries ON categoriza...
然而,这确实有效
def categories_names
all_categories.pluck("name")
end
导致以下SQL
SELECT DISTINCT "categories"."name" FROM "categories" LEFT OUTER JOIN "categorizations" ON "categorizations"."category_id" = "categories"."id" INNER JOIN galleries ON categorizations.categorizeable_type = 'Gallery' AND categorizations.categorizeable_id = galleries.id WHERE (galleries.galleriable_id = 1093 AND galleries.galleriable_type = 'Brand')
有没有办法让all_categories
没有采摘?