0

我有一个包含一系列贡献者的嵌入式文档。Contributors 是对文档做出贡献的人员的用户 ID 数组。

field :contributors, type: Array, :default => []

这是控制台中的示例项目:

#<Item _id: 5249d5bd06387b91a600000f, name: "Collapse", contributors: ["51db6d58bd02861e96000004", "51db6d58bd02861e96000004"], count: 2>

我希望能够从数组中删除项目中的第一个匹配贡献者,但是每次我尝试测试只是为了查看用户的 id 是否存在于贡献者数组中,当它明显存在时它会返回 false。

这是一个例子:

contributors.include?("51db6d58bd02861e96000004")
 => false 

我如何处理来自 Mongoid 的数组值?为什么这返回错误?

4

1 回答 1

2

贡献者是一个字符串数组还是 ObjectIds?

请尝试:

contributors.include?( Moped::BSON::ObjectId.from_string "51db6d58bd02861e96000004")

我真的不知道你的整个应用程序。但是你考虑过这种方法吗?

class Item
 has_and_belongs_to_many :users, foreign_key: :contributors
end
于 2013-10-01T09:34:49.137 回答