如果没有pluck,Rails查询不起作用

时间:2014-08-28 10:20:16

标签: sql ruby-on-rails postgresql ruby-on-rails-4

我有以下查询

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没有采摘?

0 个答案:

没有答案