- Project has_many :items
- Item belongs_to :project
I’m trying to get the projects sorted by the total price of their respective items. Something like:
Project.includes(:items).order('SUM(items.price)')
With this code, ActiveRecord returns only the first project. What am I missing?
Above SQL should work well on most DB systems (MySQL, PostgreSQL, …).
AR record
includesis mostly used as eager loading solution. And I am not sure if you can use it that way – to order records of parent table.