mongoid/mongoid — Issue #1383: Mongoid::Fields::Serializable breaks inheritance rules
Author: mkscrg
Created: 2011-10-28 17:11:40+00:00
State: open

Define a custom field type:

class MyField
include Mongoid::Fields::Serializable

attr_accessor :my_var

def initialize(var)
@my_var = var
end

def deserialize(object)
self.class.new(object['my_var'])
end

def serialize(object)
{'my_var' => object.my_var}
end
end

and then subclass it:

class MySubfield < MyField
end

When used in a model like this:

class MyDoc
include Mongoid::Document

field :field, :type => MyField
field :subfield, :type => MySubfield
end

the `subfield` field is not deserialized according to the method defined in `MyField`:

>> x = MyDoc.new(:field => MyField.new("hello"), :subfield => MySubfield.new("world"))
>> x.field
=> #
>> x.subfield
=> {"my_var" => "world"}