MySQL にこのクエリがあり、これを Rails アクティブ レコードに変換する必要があります。
select p.*,x.* from
(select * from users ,roles_users
where users.id=roles_users.user_id and roles_users.role_id not in(2,3)) as P
left join
(select *
from cad_dispaths as T
where `startdate_dispatch` =
( select max(`startdate_dispatch`)
from cad_dispaths
where users_id = T.users_id )) x
on x.users_id = p.user_id