开发者

Mongoid equivalent of ActiveRecord's `serialize` method

hope the title's pretty self-explanatory.

I'm using mongoid as my ORM for a Rails app, and I was wondering if anybody knew if it has an equivalent to ActiveRecord's serialize meth开发者_运维技巧od. I've looked through the mongoid documentation but haven't been able to find anything yet.

Here's an example of the model:

class Foo
  include Mongoid::Document

  field :params, type: String
  serialize :params            # method from ActiveRecord
end

Thanks in advance!


You don't need to serialize with MongoDB as far as you can store in fields Arrays and Hashes.

field :hash_params, type: Hash
field :array_params, type: Array


Sometimes you need use the Value Object pattern and the same function like composed_of, some people wants to deprecate this function in the future and than you want to use serialize of standard active record. Mongoid provide the same functionality to create Value object avoid the serialize method look , you can provide your custom serialization here http://mongoid.org/en/mongoid/docs/documents.html#custom_fields:

class Foo
  include Mongoid::Document

  field :params, type: String
  field :custom_params , type: MyCustomParamsType
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜