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

"Cannot apply $push/$pushAll modifier to non-array" error

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.2
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      class Foo
        include Mongoid::Document
      
        field :pin,         type: String
        field :correct_pin, type: Boolean, default: false
      
        embedded_in :bar
      
        validate :valid_pin
      
        private
      
        def valid_pin
          return if correct_pin
          if validate_pin
            update_attributes(correct_pin: true, pin: nil)
          else
            errors.add(:pin, "Incorrect PIN")
          end
        end
      
        def validate_pin
          true
        end
      end
      
      class Bar
        include Mongoid::Document
      
        embeds_many :foos
      end
      

      If I run the following code:

      b = Bar.create
      Foo.create(pin: "1234", bar: b)
      

      I get this error on the Foo.create:

      Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
        @length=90
        @request_id=7
        @response_to=0
        @op_code=2004
        @flags=[]
        @full_collection_name="example_development.$cmd"
        @skip=0
        @limit=-1
        @selector={:getlasterror=>1, :safe=>true}
        @fields=nil>
      failed with error 10141: "Cannot apply $push/$pushAll modifier to non-array"
      
      See https://github.com/mongodb/mongo/blob/master/docs/errors.md
      for details about this error.
      

      However, it appears that it does save successfully:

      b.foos #=> [#<Foo _id: 5434497f8d46b9cc35000002, pin: nil, correct_pin: true>]
      b.reload #=> #<Bar _id: 5434497f8d46b9cc35000001, >
      b.foos #=> [#<Foo _id: 5434497f8d46b9cc35000002, pin: nil, correct_pin: true>]
      

      This is using Mongoid 3.1.6 in a Rails 3.2 app; haven't tested Mongoid 4 or master yet.

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

              Created:
              Updated:
              Resolved: