-1

I use two instance variables like bellow in the view:

@conversations_user = Conversation.user(current_user.id)
@conversations_professional = Conversation.professional(professional.user.id)

I need to combine these two variables into one @conversation_all, and later order them by the time of the last message.

Is it possible to combine @conversation_user and @conversation_professional into @conversation_all?

sawa
  • 165,429
  • 45
  • 277
  • 381
Léo Rocha
  • 324
  • 3
  • 13

1 Answers1

0

You can do it in SQL with the or query. See this answer for details but essentially.

@conversation_all = Conversation.user(current_user.id).or(Conversation.professional(professional.user.id))

If you already have them loaded on the ruby side, i.e. the SQL hit has been made, you can just combine the two with a union operation.

@conversation_all = @conversations_user | @conversations_professional 

Although, keep in mind that this would be done in ruby and less than optimal than doing it in pure SQL. However doing it in ruby, once you have the data in memory, doing it in SQL is moot.

engineerDave
  • 3,887
  • 26
  • 28