Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby将参数传递到内部方法_Ruby_Ruby On Rails 5 - Fatal编程技术网

ruby将参数传递到内部方法

ruby将参数传递到内部方法,ruby,ruby-on-rails-5,Ruby,Ruby On Rails 5,我是编程新手,我在做rails。这里我有一个红宝石问题 我有一种方法,它把信息和对话作为论据 def counterpart(message, conversation) if message.author_id == conversation.sender_id counterpart_id = conversation.receiver_id else counterpart_id = conversation

我是编程新手,我在做rails。这里我有一个红宝石问题

我有一种方法,它把信息和对话作为论据

    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中,我想在中调用对方方法来获取变量的对方_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

谢谢

我们对你们的型号了解不够。所以,据我所知,让代码正常工作的最好方法是

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

您正在将参数
消息
对话
传递给
对应方
——可能您输入了一个错误,但正如所写,我不明白您在说什么asking@dax但它没有将新通知保存到我的数据库中。我不知道为什么…假设
通知
与消息有某种关系,请尝试使用
调用create方法-如果出现错误,将引发错误:
message.create\u通知