操作电缆会话。之间无法正常运行

时间:2018-06-05 18:36:32

标签: ruby-on-rails sqlite messaging actioncable

所以,我有一个允许两个用户互相发送消息的Web应用程序。 “收件人”和“发件人”。当两个用户首先尝试互相发送消息时,它的效果很好。但是,如果用户尝试向另一个人发送消息,则Action Cable不会创建新会话,而是将其返回到与第一个人进行的对话。发生这种情况是因为我的“if Conversation.between”在它应该返回FALSE时返回true!我在下面有一个截图:(忽略'&',这只是为了得到一个错误)

会话的图片。当它应该为FALSE时返回true:

enter image description here

无论如何我的代码在下面,如果有人能解决这个问题,我一直在处理它会非常感激。感谢

对话控制器:

class ConversationsController < ApplicationController
before_action :authenticate_user!

def index
 @conversations = Conversation.involving(current_user)
end

def create
 if Conversation.between(params[:sender_id],params[:recipient_id]).present?
  @conversation = Conversation.between(params[:sender_id], params[:recipient_id]).first
else
  @conversation = Conversation.create(conversation_params)
end

redirect_to conversation_messages_path(@conversation)
end

private

def conversation_params
  params.permit(:sender_id, :recipient_id)
end
end

Messages_controller:

class MessagesController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation

def index
 if current_user == @conversation.sender || current_user == @conversation.recipient
  @other = current_user == @conversation.sender ? @conversation.recipient : @conversation.sender
  @messages = @conversation.messages.order("created_at DESC")
 else
  redirect_to conversations_path, alert: "You don't have permission to view this."
 end
 end

 def create
  @message = @conversation.messages.new(message_params)
  @messages = @conversation.messages.order("created_at DESC")

 if @message.save
  ActionCable.server.broadcast "conversation_#{@conversation.id}", message: render_message(@message)
  redirect_to conversation_messages_path(@conversation)
 end
 end

 private

  def render_message(message)
   self.render(partial: 'messages/message', locals: {message: message})
  end

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

  def message_params
   params.require(:message).permit(:conteny, :user_id)
  end
  end

会话模型:

class Conversation < ApplicationRecord
belongs_to :sender, foreign_key: :sender_id, class_name: "User"
belongs_to :recipient, foreign_key: :recipient_id, class_name: "User"

has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :recipient_id

scope :involving, -> (user) {
 where("conversations.sender_id = ? OR conversations.recipient_id = ?", user.id, user.id)
 }

scope :between, -> (user_A, user_B) {
 where("(conversations.sender_id = ? OR conversations.recipient_id = ?) OR conversations.sender_id = ? OR conversations.recipient_id = ?", user_A, user_B, user_B, user_A)
 }
end

消息模型:

class Message < ApplicationRecord
belongs_to :user
belongs_to :conversation

validates_presence_of :content, :conversation_id, :user_id
after_create_commit :create_notification

def message_time
 self.created_at.strftime('%B %d, %Y')
end

 private

  def create_notification
   if self.conversation.sender_id == self.user_id
    sender = User.find(self.conversation.sender_id)
    Notification.create(content: "New message from #{sender.fullname}", user_id: self.conversation.recipient_id)
   else
    sender = User.find(self.conversation.recipient_id)
    Notification.create(content: "New message from #{sender.fullname}", user_id: self.conversation.sender_id)
   end
 end
 end

请帮助,我已经挣扎了大约一个星期了。感谢。

1 个答案:

答案 0 :(得分:0)

假设你的范围:

Conversation.between(user_A, user_B)

...表示&#34;找到user_A和user_B相互发送的所有对话记录(无论发件人是哪一个),然后......

以下内容应该有效:

scope :between, -> (user_a_id, user_b_id) {
  where('(sender_id = ? AND recipient_id = ?) OR (sender_id = ? AND recipient_id = ?)', user_a_id, user_b_id, user_b_id, user_a_id)
}

与以下内容相同:

scope :between, -> (user_a_id, user_b_id) {
  where(
    sender_id: user_a_id, recipient_id: user_b_id
  ).or(
    where(
      sender_id: user_b_id, recipient_id: user_a_id
    )
  )
}