ruby将参数传递到内部方法
我是编程新手,我在做rails。这里我有一个红宝石问题 我有一种方法,它把信息和对话作为论据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
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通知代码>