我试图在rails应用程序中实现基本信使系统。我正在关注一位在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
目标是仅在没有为同一两个用户打开时创建新会话,否则会创建一个新会话。
我尝试使用此代码,但它返回错误:
undefined method `between?' for nil:NilClass
尽管@conversations不是零。方法之间?似乎没有。
我试图在
下面编写一个错综复杂的ActiveRecod查询 @session = Conversation.where('recipient_id = params[:sender_id] and sender_id = params[:recipient_id]') + Conversation.where('recipient_id = params[:recipient_id] and sender_id = params[:sender_id]')
但它提出了
PG::UndefinedColumn: ERROR: column "params" does not exist LINE 1: ...ons".* FROM "conversations" WHERE (recipient_id = params[:se... ^ : SELECT "conversations".* FROM "conversations" WHERE (recipient_id = params[:sender_id] and sender_id = params[:recipient_id]) LIMIT $1
我的整个控制器如下:
class ConversationsController < ApplicationController
def index
@users = User.all
@profiles = Profile.all
@conversations = Conversation.all
end
def create
@session = Conversation.where('recipient_id = params[:sender_id] and sender_id = params[:recipient_id]') + Conversation.where('recipient_id = params[:recipient_id] and sender_id = params[:sender_id]')
if @session.present?
@conversation = @session.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
和我的对话模型
class Conversation < ApplicationRecord
belongs_to :sender, :foreign_key => :sender_id, class_name: 'Profile'
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'Profile'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
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
最后我的观点
<h1>conversations</h1>
<div class=”ui segment”>
<h3>Mailbox</h3>
<div class=”ui list”>
<div class=”item”>
<% @conversations.each do |conversation| %>
<% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %>
<% if conversation.sender_id == current_user.id %>
<% recipient = User.find(conversation.recipient_id) %>
<% else %>
<% recipient = User.find(conversation.sender_id) %>
<% end %>
<%= link_to recipient.firstname, conversation_messages_path(conversation)%>
<% end %>
<% end %>
</div>
</div>
</div>
<div class=”ui segment”>
<h3>All Users</h3>
<div class=”ui list”>
<% @users.each do |user| %>
<% if user.id != current_user.id %>
<div class=”item”>
<%= user.firstname %>
<%= link_to "Message me!", conversations_path(sender_id: current_user.id, recipient_id: user.id), method: :post %>
</div>
<% end %>
<% end %>
</div>
</div>
我希望有人可以就特定查询语法引发错误的原因发表评论。 感谢
答案 0 :(得分:0)
我会使用merge
查询检查两个人之间是否有对话。
我猜您有sender
和receiver
这样的课程,例如User
?
sender = User.find(first_user)
receiver = User.find(second_user)
if ((conversations = sender.conversations.merge(receiver.conversations)).any?)
@conversation = conversations.first
else
@conversation = Conversation.create(sender: sender, receiver: receiver)
end
答案 1 :(得分:0)
我终于意识到Where查询的语法“attribute = value and attribute = value”与其他查询结合时会引发错误。不知道为什么。所以我转向传统的哈希语法“属性:值”,它起作用了。