基于Ruby关联模型属性的模型排序记录

时间:2019-05-14 12:06:16

标签: ruby-on-rails ruby

我有一个库存模型和一个供应模型,而库存属于供应模型。

我想根据供应模型的“ date_received”属性对库存记录进行排序

Stock.where("seller_id = ? AND status = ? AND product_id = ?", @seller_id, "Approved", list_item.product_id)

我在以下方面遇到错误:

Stock.includes(:supply).where("seller_id = ? AND status = ? AND product_id = ?", @seller_id, "Approved", list_item.product_id).order("supply.date_received")

1 个答案:

答案 0 :(得分:2)

应该是supplies而不是supply试试,

Stock.includes(:supply).where("seller_id = ? AND status = ? AND product_id = ?", @seller_id, "Approved", list_item.product_id).order("supplies.date_received")