Project.find_by_sql " select projects.* from projects left join ( select project_id, sum(price) as items_sum from items group by project_id) as sums on project.id = sums.project_id order by sums.items_sum
Above SQL should work well on most database systems (MySQL, PostgreSQL, ...).
The AR includes entry is mainly used as a reliable boot solution. And I'm not sure if you can use it this way - to arrange the records of the parent table.
source share