【发布时间】:2014-06-13 13:36:19
【问题描述】:
我有一个 Web 应用程序,我将用户类型存储为哈希字段。我有很多收藏。处理该哈希字段的最佳方法是什么。
class Community
include Mongoid::Document
class UserRoles
Admin = :a
Moderator = :m
end
include Mongoid::Timestamps
field :un, as: :username, type: String
field :nm, as: :name, type: String
field :suids, as: :special_user_ids, type: Hash
has_and_belongs_to_many :users
end
现在可以有很多社区,有很多用户,每个用户都有很多版主。因此,对于特殊用户,我将其存储为哈希。
现在我将如何计算当前登录的用户是否是任何社区的版主/管理员
我试过了,显然这不起作用。如果我有一个文件要处理,它会起作用。而且我的解决方案不符合为什么首先使用哈希的目的。
# suids = []
# @communities = Community.all
# suids << @community.special_user_ids.collect { |k, v| k }
# //code doesnt find special_user_ids as it has too many documents to deal with.
# communities_path(@community.id)
我有一个工作代码来查找给定社区的所有管理员/版主。
def admins
@community = Community.find(params[:id])
@users = User.where(:id.in => @community.special_user_ids.collect { |k, v| k })
end
【问题讨论】:
标签: ruby-on-rails mongodb hash mongoid3