-1
我需要获取对象的所有属性。我知道有一个方法attributes
,但它不会返回为零的属性。 例如:如何获取对象的所有属性,包括那些零?
class User
include Mongoid::Document
field :name
field :email
field :age
end
u = User.new(email: '[email protected]', name: 'foo')
u.save
u.attributes # {'email' => '[email protected]', 'name' => 'foo'}
我需要u.attributes
返回{'email' => '[email protected]', 'name' => 'foo' 'age' => nil}
有一个方法as_json
这我想要做什么,但它是慢了很多。速度非常重要。