2013-10-19 17 views
5

考慮這個例子:Mongoid如何知道字符串值和符號值之間的區別?

> x = User.first # or any persisted Mongoid::Document 
=> #<User _id: 52014532a6356d1ac9000001, ...> 
> x.set :foo, :bar 
=> :bar 
> x.set :foo2, 'bar' 
=> "bar" 

注意, 「foo」 和 「foo2的」 沒有在聲明Ruby的任何地方。

然後,在MongoDB的外殼:

> db.users.findOne({_id: ObjectId('52014532a6356d1ac9000001')})  
{ 
    "_id" : ObjectId("52014532a6356d1ac9000001"), 
    "foo" : "bar", 
    "foo2" : "bar", 
    ... 
} 

但現在,早在紅寶石:

> x = User.find x.id; nil # to clear out any possibility of metadata on the instance 
=> nil 
> [x.read_attribute(:foo), x.read_attribute(:foo2)] 
=> [:bar, "bar"] 

它是如何知道的?

回答

相關問題