Ruby on rails 如何使Rails中的消息仅由两个用户(私人消息)读取?

Ruby on rails 如何使Rails中的消息仅由两个用户(私人消息)读取?,ruby-on-rails,ruby,mongoid,Ruby On Rails,Ruby,Mongoid,我正在使用RubyonRails和Mongoid 我需要MessagesController中的消息仅由发送消息的用户和消息的接收者读取,也就是说,实际上是两个用户之间的私人消息 我想我需要通过scope或者在_操作之前完成它,也许为此我需要添加一个额外的字段来存储两个用户的id 如果你能帮助我,我会很高兴的 message.rb class Message include Mongoid::Document include Mongoid::Timestamps::Created

我正在使用RubyonRails和Mongoid

我需要MessagesController中的消息仅由发送消息的用户和消息的接收者读取,也就是说,实际上是两个用户之间的私人消息

我想我需要通过scope或者在_操作之前完成它,也许为此我需要添加一个额外的字段来存储两个用户的id

如果你能帮助我,我会很高兴的

message.rb

class Message
  include Mongoid::Document
  include Mongoid::Timestamps::Created

  field :body, type: String
  field :read,  type: Mongoid::Boolean, default: false  
  field :is_deleted, type: Mongoid::Boolean, default: false
  field :conversation_id, type: BSON::ObjectId
  field :sender_id, type: BSON::ObjectId
  field :receiver_id, type: BSON::ObjectId

  belongs_to :conversation
  belongs_to :sender, class_name: 'User', foreign_key: 'sender_id'
  belongs_to :receiver, class_name: 'User', foreign_key: 'receiver_id'

  validates_presence_of :body, :conversation_id, :sender_id, :receiver_id

  index({ conversation_id: 1 }, { name: 'index_messages_on_conversation_id', background: true })
  index({ user_id: 1 }, { name: 'index_messages_on_user_id', background: true })

  def message_time
    created_at.strftime("%d/%m/%y at %l:%M %p")
  end
end
class MessagesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_conversation, only: [:index, :create]
  before_action :correct_user, only: [:index]
  
  def index
    @conversation.messages.where(receiver_id: current_user.id, read: false).update_all(read: true)
    @message = @conversation.messages.new
  end

  def create
    @message = @conversation.messages.new(message_params.except(:recipient_ids))
    @message.sender_id = current_user.id
    @message.receiver_id = @conversation.recipient(current_user)
    @message.save!

  end

  private

  def message_params
    params.require(:message).permit(:body)
  end

  def set_conversation
    @conversation = Conversation.find(params[:conversation_id])
  end

  def correct_user
    redirect_to root_path if @conversation.messages.any_of({sender_id: current_user.id}, {receiver_id: current_user.id}) == current_user._id
  end
end
class Conversation
  include Mongoid::Document

  field :is_deleted, type: Mongoid::Boolean, default: false
  field :sender_id, type: Integer
  field :receiver_id, type: Integer

  belongs_to :sender, class_name: 'User', foreign_key: 'sender_id'
  belongs_to :receiver, class_name: 'User', foreign_key: 'receiver_id'
  has_many :messages, dependent: :destroy


  validates_uniqueness_of :sender_id, scope: :receiver_id

  scope :between, -> (sender_id, receiver_id) do
    any_of({sender_id: sender_id, receiver_id: receiver_id}, {sender_id: receiver_id, receiver_id: sender_id})
  end


  def recipient(current_user)
    self.sender_id == current_user.id ? self.receiver : self.sender
  end
end
消息\u controller.rb

class Message
  include Mongoid::Document
  include Mongoid::Timestamps::Created

  field :body, type: String
  field :read,  type: Mongoid::Boolean, default: false  
  field :is_deleted, type: Mongoid::Boolean, default: false
  field :conversation_id, type: BSON::ObjectId
  field :sender_id, type: BSON::ObjectId
  field :receiver_id, type: BSON::ObjectId

  belongs_to :conversation
  belongs_to :sender, class_name: 'User', foreign_key: 'sender_id'
  belongs_to :receiver, class_name: 'User', foreign_key: 'receiver_id'

  validates_presence_of :body, :conversation_id, :sender_id, :receiver_id

  index({ conversation_id: 1 }, { name: 'index_messages_on_conversation_id', background: true })
  index({ user_id: 1 }, { name: 'index_messages_on_user_id', background: true })

  def message_time
    created_at.strftime("%d/%m/%y at %l:%M %p")
  end
end
class MessagesController < ApplicationController
  before_action :authenticate_user!
  before_action :set_conversation, only: [:index, :create]
  before_action :correct_user, only: [:index]
  
  def index
    @conversation.messages.where(receiver_id: current_user.id, read: false).update_all(read: true)
    @message = @conversation.messages.new
  end

  def create
    @message = @conversation.messages.new(message_params.except(:recipient_ids))
    @message.sender_id = current_user.id
    @message.receiver_id = @conversation.recipient(current_user)
    @message.save!

  end

  private

  def message_params
    params.require(:message).permit(:body)
  end

  def set_conversation
    @conversation = Conversation.find(params[:conversation_id])
  end

  def correct_user
    redirect_to root_path if @conversation.messages.any_of({sender_id: current_user.id}, {receiver_id: current_user.id}) == current_user._id
  end
end
class Conversation
  include Mongoid::Document

  field :is_deleted, type: Mongoid::Boolean, default: false
  field :sender_id, type: Integer
  field :receiver_id, type: Integer

  belongs_to :sender, class_name: 'User', foreign_key: 'sender_id'
  belongs_to :receiver, class_name: 'User', foreign_key: 'receiver_id'
  has_many :messages, dependent: :destroy


  validates_uniqueness_of :sender_id, scope: :receiver_id

  scope :between, -> (sender_id, receiver_id) do
    any_of({sender_id: sender_id, receiver_id: receiver_id}, {sender_id: receiver_id, receiver_id: sender_id})
  end


  def recipient(current_user)
    self.sender_id == current_user.id ? self.receiver : self.sender
  end
end

我通过一个简单的检查来决定,如果当前用户不是此
@对话的发送者和接收者,那么该用户将被重定向到根页面

class MessagesController < ApplicationController
  before_action :correct_user, only: [:index]

private

  def correct_user
    redirect_to root_path unless @conversation.sender_id == current_user.id || @conversation.receiver_id == current_user.id
  end
end
class messages控制器
请不要只发布代码作为答案,还要解释代码的作用以及如何解决问题。带有解释的答案通常更有帮助,质量也更好,更有可能吸引更多的选票。@Markrotterveel谢谢,我更新了答案并添加了解释