My models:
class Review < ActiveRecord::Base
belongs_to :business
class Business < ActiveRecord::Base
has_many :reviews
has_and_belongs_to_many :categories
I want to get the Reviews for businesses under a certain category:
Review.joins(:business => :categories).where(:business => {:categories => [1,2,3,4]})
The resulting query:
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)
However, I am getting the following error:
ActiveRecord::StatementInvalid: PG::Error: ERROR: missing FROM-clause entry
for table "business"
LINE 1: ...id" = "businesses_categories"."category_id" WHERE "business"...
用这个:
Review.joins(:business => :categories).where( :categories => { :id => [1,2,3,4] } )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.