获取当前用户与每个管理员之间的所有消息

时间:2017-08-28 13:34:59

标签: ruby-on-rails rails-activerecord

我有模型User

class User < ActiveRecord::Base
  has_many :sent_messages, class_name: 'DirectMessage', foreign_key: :sender_user_id
  has_many :received_messages, class_name: 'DirectMessage', foreign_key: :receiver_user_id
end

和模型DirrectMessage

class DirectMessage < ApplicationRecord
  belongs_to :sender_user, class_name: 'User', foreign_key: :sender_user_id
  belongs_to :receiver_user, class_name: 'User', foreign_key: :receiver_user_id
end

我需要编写查询来获取所有消息,其中:

sender_user_id = options[:current_user].id AND receiver_user.role = 'admin'receiver_user_id = options[:current_user].id AND sender_user.role = 'admin'

我只需要在当前用户和每个管理员之间获取所有消息。

现在,我正在尝试这样的事情:

DirectMessage.all.joins(:sender_user, :receiver_user).where('(sender_user_id = ? AND sender_user.role = ?) OR ( receiver_user_id = ? AND receiver_user.role = ?)', options[:current_user].id, 'admin', options[:current_user].id, 'admin')

但它只返回#<DirectMessage::ActiveRecord_Relation:0x007f8ead7879c0>。如果我试图从中获取第一条记录,我将收到错误:

  

*** ActiveRecord :: StatementInvalid Exception:PG :: UndefinedTable:ERROR:缺少表“sender_user”的FROM子句条目第1行:   ... r_id ='b4de3f59-b234-459a-adb1-2a6c1a135919'AND sender_use ...

请帮我写一个正确的查询。感谢。

2 个答案:

答案 0 :(得分:0)

您需要创建一个方法,将current_user值传递给您的模型,当前用户在模型中不可用,只有视图和控制器。 注意:您可以将这些方法定义为范围或方法。

class DirectMessage < ApplicationRecord
  ...

  def self.messages_from_user(user)
    where(sender_user: user, receiver_user: User.admins)
  end
end


class User < ApplicationRecord

...

  def self.admins
    where(role: 'admin')
  end
end

# then in your controller or view you can do 
@direct_messages = DirectMessage.messages_from_user(current_user)

答案 1 :(得分:-1)

我找到了一个有效的解决方案。首先,我们需要将范围添加到User模型:

class User < ActiveRecord::Base
  has_many :sent_messages, class_name: 'DirectMessage', foreign_key: :sender_user_id
  has_many :received_messages, class_name: 'DirectMessage', foreign_key: :receiver_user_id

  scope :admin, -> { where(role: 'admin') }
end

所以现在查询将如下所示:

DirectMessage.all.where('(sender_user_id = :current_user_id AND receiver_user_id IN (:admin_ids)) OR (sender_user_id IN (:admin_ids) AND receiver_user_id = :current_user_id)',
                        current_user_id: options[:current_user].id, admin_ids: User.admin.ids)

我不认为会有超过几十个管理员,所以这个解决方案非常好。