Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-1384

Mongoid::Fields::Serializable breaks inheritance rules

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.3.4
    • Affects Version/s: None
    • Component/s: None

      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
      => #<MyField:0x1037e32c8 @my_var="hello">
      >> x.subfield
      =>

      {"my_var" => "world"}

            Assignee:
            Unassigned Unassigned
            Reporter:
            mkscrg mkscrg
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: