我是编程新手,我正在做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中,我想在 HANDLE 中调用对应方法以获取变量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
谢谢
我们对您的模型了解不够。所以,据我所知,让你的代码工作的最佳方法是移动到
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