使用RubyOnRails中的ActiveRecord查询方法编写具有多个关联的查询时出错

时间:2017-02-24 17:49:39

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

我在使用RubyOnRails ActiveRecord查询方法编写查询时遇到问题。我有以下模型协会:

订购多对多OrderProduct多对多产品一对多ProductLocation

我想要的是获取产品位置名称='something'的所有订单(名称是ProductLocation表中的列)

我如何存档?目前我有这个

Order.joins(order_products: [{product: :product_location}]).select('orders.*, product_locations.name as location').where(product_locations: {name: 'NY'})

但它会抛出错误

SELECT COUNT(orders.*, product_locations.name as location) FROM "orders" INNER JOIN "order_products" ON "order_products"."order_id" = "orders"."id" INNER JOIN "products" ON "products"."id" = "order_products"."product_id" INNER JOIN "product_locations" ON "product_locations"."id" = "products"."product_location_id" WHERE "product_locations"."name" = 'NY'
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR:  syntax error at or near "as"
LINE 1: SELECT COUNT(orders.*, product_locations.name as location) F...

我做错了什么?提前谢谢。

0 个答案:

没有答案