我得到了下面的SQL,我試圖將其轉換成活動記錄查詢方法加入不工作正常
我有以下表
Customers (id, name)
CustomerPriceRelations (customer_id, sales_price_id) # jointable
SalesPrices (id, margin)
ProductSalesPrices (product_id, sales_price_id) # jointable
,我試圖執行下面的查詢
SELECT sp.id, sp.margin_percentage
FROM sales_prices sp
LEFT OUTER JOIN carrier_product_Sales_Prices ps
ON sp.id = ps.sales_price_id
LEFT OUTER JOIN Customer_Price_Relations cr
ON ps.sales_price_id = cr.sales_price_id
LEFT OUTER JOIN Customers c
ON cr.customer_id = c.id
WHERE c.id = 2 AND ps.carrier_product_id = 3
我試過使用連接方法,但結果從來沒有加入由於某種原因,任何幫助嗎?
SalesPrice.joins(:carrier_products, :customers).where(customer_id: 2, carrier_product_id: 3)
「結果永遠不會加入」結果是什麼?你需要更具體地瞭解什麼是和不在工作。 – 2014-09-21 15:16:47
當我加入SalesPrice表中的表時,沒有新字段被添加到結果 – Tarlen 2014-09-21 15:22:52