检查bson数组是否包含字符串id

时间:2018-05-02 18:54:21

标签: ruby-on-rails ruby ruby-on-rails-3 mongoid

我想知道一个bsons数组是否包含字符串id,如下所示:

[186] pry(main)> changed = [BSON::ObjectId('5ae9972ea8abbc3c5484cd5e')] 
=> [BSON::ObjectId('5ae9972ea8abbc3c5484cd5e')]
[187] pry(main)> changed
=> [BSON::ObjectId('5ae9972ea8abbc3c5484cd5e')]
[188] pry(main)> manager_to_remove = "5ae9972ea8abbc3c5484cd5e"
=> "5ae9972ea8abbc3c5484cd5e"
[189] pry(main)> manager_to_remove
=> "5ae9972ea8abbc3c5484cd5e"
[190] pry(main)> changed.include?(manager_to_remove).inspect
=> "false"

我总是变得虚假。

谢谢!

1 个答案:

答案 0 :(得分:1)

manager_to_remove转换为BSON::ObjectId有效:

require 'bson'
changed = [BSON::ObjectId('5ae9972ea8abbc3c5484cd5e')]
manager_to_remove = BSON::ObjectId("5ae9972ea8abbc3c5484cd5e")
changed.include?(manager_to_remove).inspect # => "true"