As you have included the :messages
on your query, you have all messages loaded, so you don't really need another query to check if it is unread and if all the conversation messages are unread. You can create methods that check these states:
def has_unread_messages?(user)
unread = self.messages.collect {|m| m.read_at.nil? && m.receiver_id == user.id}
!unread.empty?
end
All of your messages inside the @conversation.messages
are preloaded.
Updated
Here is updated solution:
def has_unread_messages?(user)
messages.any? { |message| message.read_at.nil? && message.receiver_id == user.id }
end