Sort ActiveRecords and related records