Mongoid::Criteria 和数组

因为 Mongoid::Criteria 的实例可以响应一些迭代器方法,所以有时候会让人产生错觉。它在有些情况下会自动加载数据,有的时候则不会,在完全掌握这东西之前,一定避免依赖于延迟加载、自动类型转换等东西,因为看起来会很有迷惑性:

1.9.3p194 :045 > users = UserMongo.in(id: [4017])
 => translation missing: zh-CN.mongoid.inspection.criteria 
1.9.3p194 :046 > users.class
 => Mongoid::Criteria 
1.9.3p194 :047 > users << UserMongo.last
 => [#<UserMongo _id: 4017,...>] 
1.9.3p194 :048 > users.count
 => 1 
1.9.3p194 :049 > users.class
 => Mongoid::Criteria 


1.9.3p194 :053 > users = UserMongo.in(id: [4017])
 => translation missing: zh-CN.mongoid.inspection.criteria 
1.9.3p194 :054 > users.class
 => Mongoid::Criteria 
1.9.3p194 :055 > users += [UserMongo.last]
 => [#<UserMongo _id: 4017,...>, #<UserMongo _id: 46129712,...>] 
1.9.3p194 :056 > users.count
 => 2 
1.9.3p194 :057 > users.class
 => Array 

你可能感兴趣的:(Ruby,mongoid)