Her er, hvordan jeg inkluderede alle de nødvendige modeller, denne forespørgsel er oversat til 5 sql-forespørgsler, da preload ikke slutter sig (kører i en separat forespørgsel).
Message.joins("LEFT JOIN messages AS m ON messages.id != m.id
AND m.conversation_id = messages.conversation_id
AND messages.created_at < m.created_at")
.where('m.id IS NULL')
.joins("INNER JOIN conversation_participants AS cp
ON cp.conversation_id = messages.conversation_id
AND cp.user_id = #{user_id}")
.order("cp.seen, cp.updated_at DESC")
.limit(5)
.includes(:sender)
.includes(conversation: [{conversation_participants: :user}])