2017-03-21 8 views
0

私はプログラミングが初めてで、私はレールをやっています。ここで私はルビーの質問があります。ruby​​は内部メソッドに引数を渡します

私は、引数

def counterpart(message, conversation) 
     if message.author_id == conversation.sender_id 
      counterpart_id = conversation.receiver_id 
     else 
      counterpart_id = conversation.sender_id 
     end 
    end 

及び方法のcreate_notification以下のようにメッセージや会話を取る方法の対応を持って、私は変数counterpart_idを取得するために、内相手メソッドを呼び出したいです。しかし、ここで議論のメッセージと会話をどのように相手に伝えることができますか?

def create_notification(message, conversation) 
     counterpart(message, conversation) 
     notification = message.create_notification(
        notifier_id: message.author_id, 
        receiver_id: counterpart_id, 
        content: "#{message.author.name} sent you a message", 
        title: "message") 
    end 

おかげ

+0

に移動されますがcounterpart' 'に引数' message'でと 'conversation'を渡している - 多分あなたはタイプミスを作りました、しかし書かれたように、私はあなたが何を求めているのか理解していません – dax

+0

@daxしかし、それは私のdbに新しい通知を保存しませんでした。私はなぜか分からない... – Rosalie

+0

'Notification'とメッセージとの関係があると仮定すると、何か問題が起きたときにエラーをスローする'! 'でcreateメソッドを呼び出してみてください:' message.create_notification! ' – dax

答えて

0

私たちはあなたのモデルについて十分に知りません。だから、私の知る限りでは、あなたのコードが動作するようにする最良の方法は、

def counterpart(message, conversation) 
    message.author_id == conversation.sender_id ? conversation.receiver_id : conversation.sender_id 
end 

def create_notification(message, conversation) 
    message.create_notification(notifier_id: message.author_id, 
     receiver_id: counterpart(message, conversation), 
     content: "#{message.author.name} sent you a message", 
     title: "message") 
end 
関連する問題