2016-10-14 8 views
0

私はレールアプリで基本的なメッセンジャーシステムを実装しようとしています。私はcreateメソッドで次のコードを提案したガイドに従っていました。レール内の論理演算子で2つのActiveRecord .whereクエリを評価する

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 

目的は、同じ2人のユーザーに対してまだ開いていないときだけ新しい会話を作成することです。それ以外の場合は、新しいユーザーを作成します。

私はこのコードを使用しようとしたが、それはエラーを返します:@conversationsにもかかわらず

undefined method `between?' for nil:NilClass 

がnilではありません。その間の方法?利用可能ではないようです。

私は

@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]') 

下の複雑ActiveRecodのクエリを記述しようとしたが、それは下に

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 

私の全体のコントローラを提起:私の会話モデル

class ConversationsController < 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 

class Conversation < ApplicationRecord 

    belongs_to :sender, :foreign_key => :sender_id, class_name: 'Profile' 
    belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'Profile' 
    has_many :messages, dependent: :destroy 
    validates_uniqueness_of :sender_id, :scope => :recipient_id 

    scope :between, -> (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 

最後に私の見解

<h1>conversations</h1> 

<div class=”ui segment”> 
    <h3>Mailbox</h3> 
    <div class=”ui list”> 
     <div class=”item”> 
      <% @conversations.each do |conversation| %> 
       <% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %> 
        <% if conversation.sender_id == current_user.id %> 
        <% recipient = User.find(conversation.recipient_id) %> 
       <% else %> 
        <% recipient = User.find(conversation.sender_id) %> 
       <% end %> 
      <%= link_to recipient.firstname, conversation_messages_path(conversation)%> 
      <% end %> 
      <% end %> 
     </div> 
    </div> 
</div> 

<div class=”ui segment”> 
    <h3>All Users</h3> 
    <div class=”ui list”> 
     <% @users.each do |user| %> 
     <% if user.id != current_user.id %> 
     <div class=”item”> 
      <%= user.firstname %> 
      <%= link_to "Message me!", conversations_path(sender_id: current_user.id, recipient_id: user.id), method: :post %> 
     </div> 
     <% end %> 
     <% end %> 
    </div> 
</div> 

私は、誰かがその特定のクエリ構文にエラーが発生する理由にコメントを提供することができます願っていました。 ありがとう

答えて

0

mergeクエリを使用して2人の人が会話しているかどうかを確認します。

senderのクラスとreceiverUserのようなクラスがありますか?

sender = User.find(first_user) 
receiver = User.find(second_user) 

if ((conversations = sender.conversations.merge(receiver.conversations)).any?) 
    @conversation = conversations.first 
else 
    @conversation = Conversation.create(sender: sender, receiver: receiver) 
end 
+0

こんにちはDavidありがとうございます。あなたの提案をどのように実装するか分かりません。私は送信者と受信者のIDをユーザー名のリストから取得しています。これらのパラメータはurlに渡され、ここで使用しようとしているものです。私は完全なコントローラを貼り付けて、私の質問を編集して、おそらくもっと意味をなさないでしょう。 – alopez02

0

私は最終的に他のクエリと組み合わせるクエリは、エラーが発生しどこの構文は、「属性=値と属性=値」ことに気づきました。理由は分かりません。だから私は伝統的なハッシュ構文 "属性:値"に移動し、それは働いた。