我在使用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...
我做错了什么?提前谢谢。