Message has_many user_messages. It has two.
1 UserMessage will have the sender as user_id
The other UserMessage will have the receiver as user_id
Knowing one user, how can I, for any message, find the other?
Here’s what I tried as a method to the Message class, but it fails:
32 def other_user(one_user)
33 um = self.user_messages
34 um.each do |user_message|
35
36 output_user = User.find(user_message.user_id) unless user_message.user_id == one_user.id
37 end
38
39 return output_user
40 end
This could be implemented as an association enhancement. This will be efficient for high-volume of messages as it pushes processing to DB.