简体   繁体   English

如何在mailboxer_notifications表中设置user_type?

[英]How can I set the user_type in the mailboxer_notifications table?

My problem is that when users reply to each other, it's hard examine the database via SQL and differentiate between user types (so admin, and individual users). 我的问题是,当用户相互回复时,很难通过SQL检查数据库并区分用户类型(因此,管理员和单个用户)。 The default is just "User", as a string. 默认值为字符串形式的“ User”。

When examining the mailboxer code, I know it calls the "reply" method in the Conversations controller. 当检查邮箱代码时,我知道它在“对话”控制器中调用了“答复”方法。 This is the relevant part of the code: 这是代码的相关部分:

def reply
  current_user.reply_to_conversation(conversation, message_params[:body])
  flash[:notice] = "Message was sent."
  redirect_to conversation_path(conversation)
end

From this, I wanted to do something like: 由此,我想做类似的事情:

conversation.messages.sender_type = params[:custom].present? ? params[:custom] : "Admin"

But this does not work, as it says "unknown sender type method". 但这是行不通的,因为它说“未知的发件人类型方法”。 I listed it that way because, based on the mail boxer gem code, messages belongs_to conversation, and the messages model is linked to the table and column value I want to change. 我之所以这样列出,是因为,基于邮件拳击手的gem代码,消息属于对话,并且消息模型已链接至要更改的表和列值。

This is the relevant code: https://github.com/mailboxer/mailboxer/blob/03543e8a06dea8c9bfb52963da8abbf1157d5d21/app/models/mailboxer/message.rb 这是相关的代码: https : //github.com/mailboxer/mailboxer/blob/03543e8a06dea8c9bfb52963da8abbf1157d5d21/app/models/mailboxer/message.rb

Based on this, what should I modify to have it so when I reply to a user, the reply action is called, and then conversations "calls" messages which itself sets the sender_type to a value I want? 基于此,我应该修改使其具有什么功能,以便在回复用户时调用回复操作,然后对话“调用”消息本身将sender_type设置为所需的值?

In the code you proposed, you are calling sender_type on the array of messages, but that method exists on the Mailboxer::Message model. 在您建议的代码中,您正在消息数组上调用sender_type ,但是该方法存在于Mailboxer::Message模型中。 As it's a polymorphic association see code of model here , the sender of the Mailboser::Message will be the user that was used for creating the message (in this case current_user ). 由于是多态关联, 请参见此处的model代码, Mailboser::Messagesender将是用于创建消息的用户(在本例中为current_user )。

So I guess what you are looking for, is to get the user_type of your sender for a specific message of a conversation. 因此,我想您正在寻找的是获取特定会话消息的发件人的user_type

So this is can be done, by doing the following (in this example let's get the user_type of the user that sent the first message of a given conversation : 因此,可以通过执行以下操作来完成此操作(在本示例中,让我们获取发送给定对话的第一条消息的用户的user_type

conversation.messages.first.sender.user_type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM