简体   繁体   English

Mongoid查询包含数组

[英]Mongoid querying inclusion Array

I am implementing user setting in my rails website where a user has control over who can send him notifications. 我正在我的rails网站中实现用户设置,用户可以控制谁可以向他发送通知。 The allowed classed of users who can send him information is stored in an array and can be set by the user. 可以向其发送信息的用户的允许类别存储在阵列中并且可以由用户设置。

How do I query users to send the notification according to the user type of the user who sent the post. 如何根据发送帖子的用户的用户类型查询用户发送通知。

I want to do something like this. 我想做这样的事情。

notifiable_users = User.all.or("notification_setting.posted_by" includes "sender.user_type")

Assuming notification_setting.posted_by is an array for the allowed user types and sender.user_type is the user_type(singular, not an array) of sender, you can simply do: 假设notification_setting.posted_by是允许的用户类型的数组, sender.user_typesender.user_type的user_type(单数,而不是数组),您可以简单地执行:

notifiable_users = User.all.or("notification_setting.posted_by" => sender.user_type)

If it is the other way around: 如果是相反的方式:

notifiable_users = User.all.or("notification_setting.posted_by".to_sym.in => sender.user_types)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM