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

:field => Array, does not accept mongoize() classes upon save

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • None

      Greetings,

      Thank you for producing Mongoid! I love it!

      I am attempting to save a series of objects (with mongoized,demonogize, evolve) into an Array. I get an error "undefined method `bson_dump'" which is at the bottom of the document. I have reproduced the issue using the Point example from the custom fields demo.

      Thank you!

      -daniel

      test.rb

          require 'mongoid'
      
          Mongoid.load!("mongoid.yml", :test)
      
          # this is the point example taken from the documentation
          class Point
            attr_reader :x, :y
      
            def initialize(x, y)
              @x, @y= x, y
            end
      
            # Converts an object of this instance into a database friendly value.
            def mongoize
              [ x, y ]
            end
      
            class << self
      
              # Get the object as it was stored in the database, and instantiate
              # this custom class from it.
              def demongoize(object)
                Point.new(object[0], object[1])
              end
      
              # Takes any possible object and converts it to how it would be
              # stored in the database.
              def mongoize(object)
                case object
                when Point then object.mongoize
                when Hash then Point.new(object[:x], object[:y]).mongoize
                else object
                end
              end
      
              # Converts the object that was supplied to a criteria and converts it
              # into a database friendly form.
              def evolve(object)
                case object
                when Point then object.mongoize
                else object
                end
              end
            end
          end
      
          # saving a class as a single field works fine
          class Single
            include Mongoid::Document
            field :location, :type => Point
          end
      
          s = Single.new 
          s.location = Point.new(1, 2)
          s.save
      
          # when saving the an array of objects, there is the error given
          class Plural
            include Mongoid::Document
            field :locations, :type => Array, :default => []
          end  
      
          p = Plural.new
          p.locations << Point.new(1, 2)
          p.locations << Point.new(3, 4)
          p.save
      

      mongoid.yml

          test: 
            sessions: 
              default: 
                hosts: 
                  - localhost:27017
                database: point_array
            options: 
              allow_dynamic_fields: false
              identity_map_enabled: true
              include_root_in_json: true
              include_type_for_serialization: true
              scope_overwrite_exception: true
              raise_not_found_error: false
              skip_version_check: false
              use_activesupport_time_zone: false
              use_utc: true
      

      error

      /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/bson/extensions/array.rb:14:in {{bson_dump': undefined method }}bson_dump' for #<Point:0x000000027c22a0 @x=1, @y=2> (NoMethodError)
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/bson/extensions/hash.rb:15:in `block in bson_dump'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/bson/extensions/hash.rb:14:in `each'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/bson/extensions/hash.rb:14:in `bson_dump'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/bson/document.rb:36:in `serialize'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/protocol/message.rb:129:in `block in serialize_documents'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/protocol/message.rb:128:in `each'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/protocol/message.rb:128:in `serialize_documents'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/protocol/message.rb:287:in `serialize'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/connection.rb:153:in `block in write'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/connection.rb:151:in `each'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/connection.rb:151:in `write'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:578:in `block (2 levels) in flush'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:138:in `ensure_connected'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:577:in `block in flush'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:592:in `logging'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:576:in `flush'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:565:in `process'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:253:in `insert'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/session/context.rb:63:in `block in insert'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/session/context.rb:109:in `block in with_node'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/cluster.rb:209:in `block in with_primary'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/node.rb:183:in `ensure_primary'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/cluster.rb:208:in `with_primary'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/session/context.rb:108:in `with_node'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/session/context.rb:56:in `insert'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/collection.rb:106:in `block in insert'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/session.rb:324:in `with'
      from /var/lib/gems/1.9.1/gems/moped-1.4.5/lib/moped/collection.rb:105:in `insert'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/persistence/operations/insert.rb:27:in `block in persist'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/persistence/insertion.rb:25:in `block (2 levels) in prepare'
      from /var/lib/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:403:in `_run*4468728994625756828*create*237314211246586726*callbacks'
      from /var/lib/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
      from /var/lib/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
      from /var/lib/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/callbacks.rb:130:in `run_callbacks'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/persistence/insertion.rb:24:in `block in prepare'
      from /var/lib/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:403:in `_run*4468728994625756828*save*237314211246586726*callbacks'
      from /var/lib/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
      from /var/lib/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
      from /var/lib/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/callbacks.rb:130:in `run_callbacks'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/persistence/insertion.rb:23:in `prepare'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/persistence.rb:51:in `insert'
      from /var/lib/gems/1.9.1/gems/mongoid-3.1.3/lib/mongoid/persistence.rb:80:in `save'
      from test.rb:66:in `<main>'

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

              Created:
              Updated:
              Resolved: