How to get current_user's conversations?

floox

I have 3 models as listed below:

Model

class Message < ApplicationRecord
  belongs_to :sender, class_name: "User"
  belongs_to :recipient, class_name: "User"
  belongs_to :conversation
end

class Conversation < ApplicationRecord
  has_many :messages
end


class User < ApplicationRecord
  has_many :recieved_messages, class_name: "Message", foreign_key: "recipient_id"
  has_many :sent_messages, class_name: "Message", foreign_key: "sender_id"
end

I tried this in user.rb:

 def get_user_conversations
    user_conversations = []

    self.recieved_messages.each do |message|
      user_conversations << message.conversation
    end

    self.sent_messages.each do |message|
      user_conversations << message.conversation
    end
  end

But I got messages instead of conversations that I wanted.

And I tried in rails console:

u1 = User.first
u1.sent_messages[0].conversation

This time got the conversation.

Here are is my question:

  1. Why does user_conversations << message.conversation not work, but u1.sent_messages[0].conversation does?

  2. What is the right way to get the conversations at this situation?

Thanks.


Maybe this way?

ids = current_user.sent_messages.pluck(:conversation_id)

Conversation.find(ids)
kolas
  1. I don't know, try to reload console. Maybe you should explicitly return user_conversations in the end of method.

  2. You can use Conversation.joins(:messages).where("messages.recipient_id = ? OR messages.sender_id = ?", current_user.id, current_user.id).distinct

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How I get all users except the current_user by username

From Dev

How to pass current_user to Sidekiq's Worker

From Dev

how can I display all users except current user and current_user's friends in ruby on rails

From Dev

how can I display all users except current user and current_user's friends in ruby on rails

From Dev

how to get facebook page conversations through Restfb ?

From Dev

How to alter Outlook's handling of replies to conversations?

From Dev

Get current_user['id'] if user is anonymous

From Dev

Activerecord-reputation-system - how to check value of current_user's vote

From Dev

How to get user's current location in Android

From Dev

How to get user's current location in Android

From Dev

Get all conversations from one user in different chats MYSQL

From Dev

How to use "current_user" with devise in Rails?

From Dev

How to access current_user object in model?

From Dev

How to use "current_user" with devise in Rails?

From Dev

How to Add Current_User to Nested Attribute?

From Dev

How to set "current_user" in Meteor

From Dev

How to route based on current_user?

From Dev

Get Conversations List Query

From Dev

Django: how to get the current user's group id

From Java

How can I get the current user's username in Bash?

From Dev

how get django current user's name both in shell and in request

From Dev

How to get the current user's role in a custom rally app in production?

From Dev

How to get current user's custom information and show it on any screen?

From Dev

Devise's current_user doesn't work?

From Dev

Create a method like Devise's current_user to use everywhere

From Dev

Which is a quicker way to fetch current_user's post

From Dev

How can I get the current Facebook user's public ID (not the scoped-id) or get the current user's profile photo

From Dev

How can I get the current Facebook user's public ID (not the scoped-id) or get the current user's profile photo

From Dev

get conversations of multiple contacts by using "content://mms-sms/conversations"

Related Related

  1. 1

    How I get all users except the current_user by username

  2. 2

    How to pass current_user to Sidekiq's Worker

  3. 3

    how can I display all users except current user and current_user's friends in ruby on rails

  4. 4

    how can I display all users except current user and current_user's friends in ruby on rails

  5. 5

    how to get facebook page conversations through Restfb ?

  6. 6

    How to alter Outlook's handling of replies to conversations?

  7. 7

    Get current_user['id'] if user is anonymous

  8. 8

    Activerecord-reputation-system - how to check value of current_user's vote

  9. 9

    How to get user's current location in Android

  10. 10

    How to get user's current location in Android

  11. 11

    Get all conversations from one user in different chats MYSQL

  12. 12

    How to use "current_user" with devise in Rails?

  13. 13

    How to access current_user object in model?

  14. 14

    How to use "current_user" with devise in Rails?

  15. 15

    How to Add Current_User to Nested Attribute?

  16. 16

    How to set "current_user" in Meteor

  17. 17

    How to route based on current_user?

  18. 18

    Get Conversations List Query

  19. 19

    Django: how to get the current user's group id

  20. 20

    How can I get the current user's username in Bash?

  21. 21

    how get django current user's name both in shell and in request

  22. 22

    How to get the current user's role in a custom rally app in production?

  23. 23

    How to get current user's custom information and show it on any screen?

  24. 24

    Devise's current_user doesn't work?

  25. 25

    Create a method like Devise's current_user to use everywhere

  26. 26

    Which is a quicker way to fetch current_user's post

  27. 27

    How can I get the current Facebook user's public ID (not the scoped-id) or get the current user's profile photo

  28. 28

    How can I get the current Facebook user's public ID (not the scoped-id) or get the current user's profile photo

  29. 29

    get conversations of multiple contacts by using "content://mms-sms/conversations"

HotTag

Archive