如何将Mailboxer收件箱限制为特定用户之间的对话



我正在创建一个消息传递系统,其中的特定用户可能是一个或多个组织的成员。因此,如果他们登录到一个组织,他们应该只能看到与来自同一组织的用户的对话,但我似乎找不到一种方法来弄清楚如何在查询中指定这一点。例如:

    recipients = current_org.users
    @conversations = current_user.mailbox.inbox.conversations.where(participants.include?(recipients))

. .或者类似的东西

我没有找到一个很好的方法来做到这一点,但这是我为将来参考所做的。我在Mailboxer的Receipts模型的初始化器文件中添加了一个class_eval,创建了一个稍微修改过的. receivers作用域。它检查集合中是否有多个集合并调用.id,这在之前是一个问题。它还获取第一个集合的base_class,因为它们都是相同的。

Mailboxer::Receipt.class_eval do
  scope :recipients, lambda { |recipient|
    if recipient.is_a?(ActiveRecord::Associations::CollectionProxy)
      where(:receiver_id => recipient.collect {|x| x.id },:receiver_type => recipient.first.class.base_class.to_s)
    else
      where(:receiver_id => recipient.id,:receiver_type => recipient.class.base_class.to_s)
    end
    }
end

最新更新