DEFINITION:
QUERY:
EXPLENATION: first join select proper contract for user and second loads propper fields for sort
to be continued..
- User table
- Contract table
- User can has many Contract
- Contract has rating field
QUERY:
User.joins('LEFT OUTER JOIN ( SELECT MAX(id) AS id, user_id FROM contracts GROUP BY user_id ) contracts ON contracts.user_id = user.id') .joins('LEFT OUTER JOIN contracts contract_fields ON contract_fields.id = contracts.id') .order('contract_fields.rating DESC NULLS LAST')
EXPLENATION: first join select proper contract for user and second loads propper fields for sort
to be continued..