title: Evaluating 2 ActiveRecord .where queries with logical operators in rails
body: <p>I'm trying to implement a basic messenger system in a rails app. I was following a guide who proposed the following code in the create method.</p>
  
  <pre><code>    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
      redirect_to conversation_messages_path(@conversation)
      end
  </code></pre>
  
  <p>The objective is to only create a new conversation when there was none already open for the same two users, otherwise it creates a new one.</p>
  
  <p>I've tried to use this code but it returns an error: </p>
  
  <pre><code>undefined method `between?' for nil:NilClass
  </code></pre>
  
  <p>despite @conversations is not nil. The method between? doesn't seem to be available. </p>
  
  <p>I've tried to write a convoluted ActiveRecod query below</p>
  
  <pre><code>    @session = Conversation.where('recipient_id = params[:sender_id] and sender_id = params[:recipient_id]') + Conversation.where('recipient_id = params[:recipient_id] and sender_id = params[:sender_id]')
  </code></pre>
  
  <p>but it raises  </p>
  
  <pre><code>PG::UndefinedColumn: ERROR: column "params" does not exist LINE 1: ...ons".* FROM "conversations" WHERE (recipient_id = params[:se... ^ : SELECT "conversations".* FROM "conversations" WHERE (recipient_id = params[:sender_id] and sender_id = params[:recipient_id]) LIMIT $1
  </code></pre>
  
  <p>my whole controller below:</p>
  
  <pre><code>class ConversationsController &lt; ApplicationController
  
  def index
      @users = User.all
      @profiles = Profile.all
      @conversations = Conversation.all
   end
  
  def create
  
      @session = Conversation.where('recipient_id = params[:sender_id] and sender_id = params[:recipient_id]') + Conversation.where('recipient_id = params[:recipient_id] and sender_id = params[:sender_id]')
  
      if @session.present?
        @conversation = @session.first
      else
        @conversation = Conversation.create!(conversation_params)
      end
        redirect_to conversation_messages_path(@conversation)
  end
  
  private
  
  def conversation_params
      params.permit(:sender_id, :recipient_id)
  end
  end
  </code></pre>
  
  <p>and my Conversation model</p>
  
  <pre><code>class Conversation &lt; ApplicationRecord
  
    belongs_to :sender, :foreign_key =&gt; :sender_id, class_name: 'Profile'
    belongs_to :recipient, :foreign_key =&gt; :recipient_id, class_name: 'Profile'
    has_many :messages, dependent: :destroy
    validates_uniqueness_of :sender_id, :scope =&gt; :recipient_id
  
    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>Finally my view</p>
  
  <pre><code>    &lt;h1&gt;conversations&lt;/h1&gt;
  
  &lt;div class=”ui segment”&gt;
      &lt;h3&gt;Mailbox&lt;/h3&gt;
      &lt;div class=”ui list”&gt;
          &lt;div class=”item”&gt;
              &lt;% @conversations.each do |conversation| %&gt;
                  &lt;% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %&gt;
                      &lt;% if conversation.sender_id == current_user.id %&gt;
                      &lt;% recipient = User.find(conversation.recipient_id) %&gt;
                  &lt;% else %&gt;
                      &lt;% recipient = User.find(conversation.sender_id) %&gt;
                  &lt;% end %&gt;
              &lt;%= link_to recipient.firstname,   conversation_messages_path(conversation)%&gt;
              &lt;% end %&gt;
              &lt;% end %&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div class=”ui segment”&gt;
      &lt;h3&gt;All Users&lt;/h3&gt;
      &lt;div class=”ui list”&gt;
          &lt;% @users.each do |user| %&gt;
          &lt;% if user.id != current_user.id %&gt;
          &lt;div class=”item”&gt;
              &lt;%= user.firstname %&gt;
              &lt;%= link_to "Message me!", conversations_path(sender_id: current_user.id, recipient_id: user.id), method: :post %&gt;
          &lt;/div&gt;
          &lt;% end %&gt;
          &lt;% end %&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I was hoping someone can provide a comment on why that particular query syntax raises an error.
  Thanks</p>