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

update attribute with non existing field

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 6.0.0
    • Affects Version/s: None
    • Component/s: None
    • None

      I have problem when updating non existing field of Game model

      My Game model is -

      class Game
      include Mongoid::Document
      field :name
      end

      When i am trying to update field number_of_players which doesn't exist in model using update_attribute

      Game.last.update_attribute(:number_of_players, 11)

      It's creating field in mongodb and my log is

      MOPED: 127.0.0.1:27017 QUERY database=game_development collection=games selector= {"$query"=>{}, "$orderby"=>{:_id=>-1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.7107ms
      MOPED: 127.0.0.1:27017 UPDATE database=game_development collection=games selector=

      {"_id"=>BSON::ObjectId('547df1f17368773b70010000')}

      update={"$set"=>{"number_of_players"=>11}} flags=[]
      COMMAND database=game_development command={:getlasterror=>1, :w=>1} runtime: 0.6224ms

      Here is the record from mongo console

      > db.games.find(

      {name: 'cricket'}

      )

      { "_id" : ObjectId("547df1f17368773b70010000"), "name" : "cricket", "number_of_players" : 11 }

      Now when i am trying to do same thing using update attributes

      Game.last.update_attributes(number_of_players: 11)

      It's raising error

      Mongoid::Errors::UnknownAttribute:
      Problem:
      Attempted to set a value for 'number_of_players' which is not allowed on the model Game.
      Summary:
      Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Game#number_of_players= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError.
      Resolution:
      You can include Mongoid::Attributes::Dynamic if you expect to be writing values for undefined fields often.
      from /home/shweta/.rvm/gems/ruby 2.1.0/gems/mongoid4.0.0/lib/mongoid/attributes/processing.rb:96:in `process_attribute'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/mongoid 4.0.0/lib/mongoid/attributes/processing.rb:25:in `block in process_attributes'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/mongoid-4.0.0/lib/mongoid/attributes/processing.rb:23:in `each_pair'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/mongoid-4.0.0/lib/mongoid/attributes/processing.rb:23:in `process_attributes'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/mongoid-4.0.0/lib/mongoid/attributes.rb:203:in `block in assign_attributes'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/mongoid-4.0.0/lib/mongoid/threaded/lifecycle.rb:26:in `_assigning'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/mongoid-4.0.0/lib/mongoid/attributes.rb:202:in `assign_attributes'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/mongoid-4.0.0/lib/mongoid/persistable/updatable.rb:51:in `update'
      from (irb):31
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:90:in `start'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands/console.rb:9:in `start'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:69:in `console'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
      from /home/shweta/.rvm/gems/ruby-2.1.0/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
      from bin/rails:4:in `require'
      from bin/rails:4:in `<main>'

      and log is

      MOPED: 127.0.0.1:27017 COMMAND database=admin command={:ismaster=>1} runtime: 0.6409ms
      MOPED: 127.0.0.1:27017 QUERY database=game_development collection=games selector={"$query"=>{}, "$orderby"=>{:_id=>-1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 0.6466ms

      is this a bug or i am missing something?

            Assignee:
            Unassigned Unassigned
            Reporter:
            Shwetakale Shwetakale [X]
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: