title: Creating a Conversation between 3 users in a rails chat app
body: <p><strong>Currently</strong>, it requires <code>recipient_id</code> and <code>sender_id</code> to start a Conversation between 2 users. </p>
  
  <p>How would the associations work if I wanted to allow a 3rd user to join a Conversation?</p>
  
  <p>When a user starts a conversation, the <code>conversation.id</code> belongs to <code>sender_id</code> and <code>recipient_id</code>. </p>
  
  <p>I want <code>visitor_id</code>(which is the <code>current_user</code>) to be able to join any conversation and all users to be able to view all conversations. </p>
  
  <p>conversation.rb </p>
  
  <pre><code>class Conversation &lt; ActiveRecord::Base
    belongs_to :sender, :foreign_key =&gt; :sender_id, class_name: 'User'
    belongs_to :recipient, :foreign_key =&gt; :recipient_id, class_name: 'User'
  
    has_many :messages, dependent: :destroy
  
    validates_uniqueness_of :sender_id, :scope =&gt; :recipient_id
  
    scope :involving, -&gt; (user) do
      where("conversations.sender_id =? OR conversations.recipient_id =?",user.id,user.id)
    end
  
    scope :between, -&gt; (sender_id,recipient_id) do
      where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
    end
  end
  </code></pre>
  
  <p>user.rb</p>
  
  <pre><code>class User &lt; ApplicationRecord
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
  
    has_many :conversations, :foreign_key =&gt; :sender_id
    after_create :create_default_conversation
  </code></pre>
  
  <p>I'm curious is if it's easy as adding </p>
  
  <pre><code>belongs_to :visitor, :foreign_key =&gt; :visitor_id, class_name: 'User'
  </code></pre>
  
  <p>to the conversation.rb model. But i'm not sure how i can get visitor_id which belongs to current_user to join a specific conversation (or make all conversations viewable to everyone).</p>
  
  <p>EDIT: Added message.rb and controllers. </p>
  
  <p>message.rb </p>
  
  <pre><code>class Message &lt; ActiveRecord::Base
    belongs_to :conversation
    belongs_to :user
  
    validates_presence_of :body, :conversation_id, :user_id
  end
  </code></pre>
  
  <p>conversations_controller.rb </p>
  
  <pre><code>class ConversationsController &lt; ApplicationController
    before_filter :authenticate_user!
  
    layout false
  
    def create
      if Conversation.between(params[:sender_id],params[:recipient_id]).present?
        @conversation = Conversation.between(params[:sender_id],params[:recipient_id]).first
      else
        @conversation = Conversation.create!(conversation_params)
      end
  
      render json: { conversation_id: @conversation.id }
    end
  
    def show
      @conversation = Conversation.find(params[:id])
      @reciever = interlocutor(@conversation)
      @messages = @conversation.messages
      @message = Message.new
    end
  
    private
    def conversation_params
      params.permit(:sender_id, :recipient_id)
    end
  
    def interlocutor(conversation)
      current_user == conversation.recipient ? conversation.sender : conversation.recipient
    end
  end
  </code></pre>
  
  <p>messages_controller.rb</p>
  
  <pre><code>class MessagesController &lt; ApplicationController
    before_filter :authenticate_user!
  
    def create
      @conversation = Conversation.find(params[:conversation_id])
      @message = @conversation.messages.build(message_params)
      @message.user_id = current_user.id
      @message.save!
  
      #@path = conversation_path(@conversation)
    end
  
    private
  
    def message_params
      params.require(:message).permit(:body)
    end
  end
  </code></pre>