我正在为我的应用程序开发一个基本的私人消息系统。我从中期开始使用this tutorial。
我发现了一个问题。它返回所有会话,而不仅仅是当前用户实际参与的会话。视图仅显示您所在的会话,但所有记录都在那里。显然,如果你有超过少数用户,那就太可怕了。
我已经将控制器调整到我认为的解决方案但我仍然获得所有记录,所以我认为问题出在模型中。
对话控制器
class ConversationsController < ApplicationController
before_action :authenticate_user!
before_action :set_conversation, only: [:destroy]
def index
@user = current_user
@conversations = Conversation.where(:sender_id == @user.id || :recipient_id == @user.id)
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
会话模型
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 :between, -> (sender_id,recipient_id) do
where(sender_id: [sender_id,recipient_id], recipient_id: [sender_id,recipient_id])
end
def unread_message_nr_for(user_id)
messages.where('messages.read = ?', false).where('messages.user_id != ?', user_id).count
end
end
对话视图
<div class="ibox-content no-side-borders">
<% @conversations.each do |conversation| %>
<div class="conversation-member">
<% 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 %>
<span class="<%= 'current-conversation' if (params['conversation_id'].to_i == conversation.id) %>">
<% if recipient.avatar.present? %>
<%= image_tag(recipient.avatar_url(:navigation), class: "img-circle m-r-xs") %>
<% end %>
<%= link_to recipient.first_name + " " + recipient.last_name, conversation_messages_path(conversation)%>
</span>
<% if conversation.unread_message_nr_for(current_user.id) > 0 %>
<span class="badge-inline">
<%= conversation.unread_message_nr_for(current_user.id) %>
</span>
<% end %>
<% end %>
</div>
<% end %>
</div>
对话架构
create_table "conversations", force: :cascade do |t|
t.integer "sender_id"
t.integer "recipient_id"
t.datetime "created_at"
t.datetime "updated_at"
end
如何只获取当前用户为sender_id或recipient_id的会话?