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

accepts_nested_attributes_for causes a Mass-Assignment notice

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

      When using

      Unable to find source-code formatter for language: accepts_nested_attributes_for```. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
       and updating an existing nesting, Mongoid fires a Mass-Assignment warning. On a more strict environment this warning might turn into exception (see example possible patch below).
      
      However it seems like ActiveRecord is removing the ID field once the record has been and retrieved from the DB so that it does not assign that same ID field again.
      
      Example code
      

      rb
      class Comment
      include Mongoid::Document
      field :msg
      belongs_to :test_user
      end

      class TestUser
      include Mongoid::Document
      has_many :comments
      accepts_nested_attributes_for :comments, allow_destroy: true
      end

      u=TestUser.create!()
      comment = u.comments.create!(msg: 'Hello')
      u.update_attributes(comments_attributes:

      {id: comment.id, msg: 'updated message'}

      )

      DEBUG WARNING: Can't mass-assign protected attributes: id

      `

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

              Created:
              Updated:
              Resolved: