将从 mongoid 查询构建的 Ruby 数组转换为实际的 mongoid 对象,或者将方法重新写入 return mongoid 对象?
Turn a Ruby Array built from a mongoid query into an actual mongoid object, or re write method to return a mongoid object?
我有一个名为 Department
的 mongoid 模型和一个名为 User
的独立模型,这两个模型之间没有原生关系。由于我的应用程序中的关系如何工作,我手动将文档 ID 存储在用户模型中。
我正在为 Ruby 使用 Grape
框架,它有一个过滤系统,位于名为 Entities
的 Mongoid 对象之上,它拒绝任何不是 mongoid 的东西查询响应对象,因为此方法 return 是一个 ruby 数组而不是 Mongoid 对象,我的框架给我错误。
有什么方法可以将我的函数重新写入 return Mongoid 对象?或者有什么方法可以将一组 Mongoid 对象转换为一个 Mongoid 对象?
## inside Department Model
def self.user_can_access(user = nil)
if user != nil
departments = []
## department_access_keys are embedded documents belonging to a user
user.department_access_keys.each do |key|
departments << BACKBONE::Department.find(key.key)
end
departments ## => returns an array of Department Documents that a user has been granted access to
else
raise 'user was not defined'
end
end
我相信,“Mongoid 对象”应该只是一个散列,所以这应该有效(另请注意 Enumerable.map
而不是 phpish each { << }
):
## inside Department Model
def self.user_can_access(user = nil)
raise 'user was not defined' if user.nil?
{
departments: # return hash here
user.department_access_keys.map do |key|
BACKBONE::Department.find(key.key)
end
}
end
你不能像这样使用find
吗?
departments = BACKBONE::Department.find(*user.department_access_keys.map(&:key))
我对 mongoid
不是很熟悉,但是 Documentation 似乎表明这正是实现您想要的方法。
Criteria#find
Find a document or multiple documents by their ids. Will raise an error by default if any of the ids do not match.
示例:
Band.find("4baa56f1230048567300485c")
Band.find(
"4baa56f1230048567300485c",
"4baa56f1230048567300485d"
)
Band.where(name: "Photek").find(
"4baa56f1230048567300485c"
)
我有一个名为 Department
的 mongoid 模型和一个名为 User
的独立模型,这两个模型之间没有原生关系。由于我的应用程序中的关系如何工作,我手动将文档 ID 存储在用户模型中。
我正在为 Ruby 使用 Grape
框架,它有一个过滤系统,位于名为 Entities
的 Mongoid 对象之上,它拒绝任何不是 mongoid 的东西查询响应对象,因为此方法 return 是一个 ruby 数组而不是 Mongoid 对象,我的框架给我错误。
有什么方法可以将我的函数重新写入 return Mongoid 对象?或者有什么方法可以将一组 Mongoid 对象转换为一个 Mongoid 对象?
## inside Department Model
def self.user_can_access(user = nil)
if user != nil
departments = []
## department_access_keys are embedded documents belonging to a user
user.department_access_keys.each do |key|
departments << BACKBONE::Department.find(key.key)
end
departments ## => returns an array of Department Documents that a user has been granted access to
else
raise 'user was not defined'
end
end
我相信,“Mongoid 对象”应该只是一个散列,所以这应该有效(另请注意 Enumerable.map
而不是 phpish each { << }
):
## inside Department Model
def self.user_can_access(user = nil)
raise 'user was not defined' if user.nil?
{
departments: # return hash here
user.department_access_keys.map do |key|
BACKBONE::Department.find(key.key)
end
}
end
你不能像这样使用find
吗?
departments = BACKBONE::Department.find(*user.department_access_keys.map(&:key))
我对 mongoid
不是很熟悉,但是 Documentation 似乎表明这正是实现您想要的方法。
Criteria#find Find a document or multiple documents by their ids. Will raise an error by default if any of the ids do not match.
示例:
Band.find("4baa56f1230048567300485c")
Band.find(
"4baa56f1230048567300485c",
"4baa56f1230048567300485d"
)
Band.where(name: "Photek").find(
"4baa56f1230048567300485c"
)