我有一个查询,它生成了一个产品数据列表;我正在更深入地挖掘,并从连接表(ProductSelections)中提取属性。RFIDTag和Products都有许多产品选择模型(即模式表包含一个rfid_tag_id和一个product_id)。由于我是从产品列表开始工作的,所以我的查询是从以下内容派生出来的:
#original query (works as needed):
@warehoused_products = Product.includes(:rfid_tags).joins(:rfid_tags).where("rfid_tags.location_type = 'Warehouse' AND rfid_tags.location_id = ?", params[:id])
#Attempt at getting product selection data:
@product_histories = @warehoused_products.joins("INNER JOIN product_selections ON :product_selections.product_id = products.id").includes(:product_selections)当我检查它时,这会引发一个相当大的postgreSQL错误:
raise @product_histories.inspect:
PG::SyntaxError: ERROR: syntax error at or near ":" LINE 1: ...eted_at" IS NULL INNER JOIN product_selections ON :product_s...在调整查询时:
raise @warehoused_products.includes(:product_selections).joins(:product_selections).where("product_selections.product_id = ?", params[:product_id]).first.inspect我得到了一个nil结果--没有错误,而且可能更干净,但并不完全是这样。
Product Selection对象如下所示,以供参考:
#<ProductSelection id: 269, user_id: 2, listing_id: 11, product_id: 35, created_at: "2016-07-14 15:38:11", updated_at: "2016-08-08 21:10:13", rfid_tag_id: 575, part_id: nil, use_time: 2173274, account_id: 1, deleted_at: nil>如何解析查询,以便拉出关联的表对象?
发布于 2018-11-20 15:05:58
@warehoused_products = Product.includes(:rfid_tags).where("rfid_tags.location_type = ? AND rfid_tags.location_id = ?", 'Warehouse', params[:id])
@product_histories = @warehoused_products.includes("product_selections").where("product_selections.product_id = ?", params[:product_id]).first.inspecthttps://stackoverflow.com/questions/53383703
复制相似问题