Sql Rails查询在没有抓取的情况下无法工作

Sql Rails查询在没有抓取的情况下无法工作,sql,ruby-on-rails,postgresql,ruby-on-rails-4,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 ga

我有以下疑问

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')

有没有一种方法可以让所有的类别都不使用Pull?

作为一个数据库开发人员,在这里购买的是tag,而不是rails人员,什么是Pull?您没有指定select。类别。包括:categorizations.joinsINNER在分类时加入图库。Categorizable_类型='Gallery'和categorizations.Categorizable_id=galleries.id.wheregalleries.galleriable_id=:brand_id和galleries.galleriable_类型='brand',brand\u id:brand\u id.选择:name.distinct您需要指定应该显示哪些字段DISTINCT@fl00r但是distinct根据应该返回唯一记录对于唯一类别记录,我应该在category.id中包含distinct?