我正在使用我的rails应用程序中的消息传递系统。我已经在2个用户(发件人和收件人)之间发送邮件正常工作。这种设置很好,但我怎样才能为每个房间进行一次新的对话,以便唯一性检查将仅在用户和房间之间进行或反之亦然?每个用户只能从房间显示页面发送消息到房间。所以room_id可以在那里找到。单个用户可能有许多列表,这使得它对我来说很复杂。所以我很困惑在下面的代码中做了什么改变来实现这个目标?或者我必须为模型做出不同的设计方法? 我有一个user
,listing
,conversation
和message
模型在rails中的私人消息传递系统
conversation.rb
class Conversation < ActiveRecord::Base
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, scope: :recipient_id
scope :involving, -> (user) do
where("conversations.sender_id = ? OR conversations.recipient_id = ?", user.id, user.id)
end
scope :between, -> (sender_id, recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id = ?) OR (conversations.sender_id = ? AND conversations.recipient_id = ?)",
sender_id, recipient_id, recipient_id, sender_id)
end
end
Message.rb
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :user
validates_presence_of :content, :conversation_id, :user_id
def message_time
created_at.strftime("%v")
end
end
conversations_controller.rb
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.rb
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
redirect_to conversation_messages_path(@conversation)
end
end
private
def set_conversation
@conversation = Conversation.find(params[:conversation_id])
end
def message_params
params.require(:message).permit(:content, :user_id)
end
end
这只是如何解决您的应用程序中的一些问题的开始。这是一个非常常见的域名,因此您可能想要使用消息传递系统来扫描github以获取开源应用程序,并检查他们是如何处理涉及的问题的。 – max
非常感谢mate ..这有助于...是不是像提到多态为真? – Abhilash
不,多态关系是你有关系的地方,关联记录的类型是动态的。就像如果你有一个可以属于“文章”或“幻灯片”的图片一样。这是一个通过连接模型的多对多关系。 http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association – max