Bhimasen Rautaray
Bhimasen Rautaray

Reputation: 301

sorting record of a model based on attribute of associated model in ruby on rails

I have a stock model and supply model and stock belongs to supply model.

I want to sort stock records based on "date_received" attribute of supply model

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

I am getting error for below:

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

Upvotes: 0

Views: 39

Answers (1)

Vishal
Vishal

Reputation: 828

there just should be supplies instead of supply Try,

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

Upvotes: 2

Related Questions