0

私のモデル:

class Review < ActiveRecord::Base 
  belongs_to :business

class Business < ActiveRecord::Base
  has_many :reviews
  has_and_belongs_to_many :categories

特定のカテゴリのビジネスのレビューを取得したい:

Review.joins(:business => :categories).where(:business => {:categories => [1,2,3,4]})

結果のクエリ:

SELECT "reviews".* FROM "reviews" INNER JOIN 
"businesses" ON "businesses"."id" = "reviews"."business_id" INNER JOIN 
"businesses_categories" ON "businesses_categories"."business_id" = "businesses"."id" 
INNER JOIN "categories" ON "categories"."id" = "businesses_categories"."category_id" 
WHERE "business"."categories" IN (1, 2, 3, 4)

ただし、次のエラーが発生します。

ActiveRecord::StatementInvalid: PG::Error: ERROR:  missing FROM-clause entry 
for table "business"
LINE 1: ...id" = "businesses_categories"."category_id" WHERE "business"...
4

1 に答える 1

3

これを使って:

Review.joins(:business => :categories).where( :categories => { :id => [1,2,3,4] } )
于 2012-11-20T21:35:45.787 に答える