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

`update_attributes` with persisted nested attribute raises `NoMethodError`

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

      Hi,

      Not sure if I'm misusing nested attributes. Here's what I'm trying to do:

      class Favorite
      include Mongoid::Document

      belongs_to :person
      end

      class Person
      include Mongoid::Document

      has_many :favorites

      accepts_nested_attributes_for :favorites
      end

      > favorite = Favorite.create
      > person = Person.create
      > person.update_attributes(favorites_attributes: [favorite.attributes])
      NoMethodError (undefined method `update_attributes' for nil:NilClass)
      from mongoid-3.0.9/lib/mongoid/relations/builders/nested_attributes/many.rb:113:in `process_attributes'
      from mongoid-3.0.9/lib/mongoid/relations/builders/nested_attributes/many.rb:29:in `block in build'
      from mongoid-3.0.9/lib/mongoid/relations/builders/nested_attributes/many.rb:27:in `each'
      from mongoid-3.0.9/lib/mongoid/relations/builders/nested_attributes/many.rb:27:in `build'
      from mongoid-3.0.9/lib/mongoid/nested_attributes.rb:55:in `block (3 levels) in accepts_nested_attributes_for'
      from mongoid-3.0.9/lib/mongoid/threaded/lifecycle.rb:26:in `_assigning'
      from mongoid-3.0.9/lib/mongoid/nested_attributes.rb:54:in `block (2 levels) in accepts_nested_attributes_for'
      from mongoid-3.0.9/lib/mongoid/attributes/processing.rb:137:in `block in process_nested'
      from mongoid-3.0.9/lib/mongoid/attributes/processing.rb:136:in `each_pair'
      from mongoid-3.0.9/lib/mongoid/attributes/processing.rb:136:in `process_nested'
      from mongoid-3.0.9/lib/mongoid/attributes/processing.rb:150:in `process_pending'
      from mongoid-3.0.9/lib/mongoid/attributes/processing.rb:32:in `block in process_attributes'
      from mongoid-3.0.9/lib/mongoid/attributes/processing.rb:190:in `with_mass_assignment'
      from mongoid-3.0.9/lib/mongoid/attributes/processing.rb:22:in `process_attributes'
      from mongoid-3.0.9/lib/mongoid/attributes.rb:158:in `block in assign_attributes'
      from mongoid-3.0.9/lib/mongoid/threaded/lifecycle.rb:26:in `_assigning'
      from mongoid-3.0.9/lib/mongoid/attributes.rb:157:in `assign_attributes'
      from mongoid-3.0.9/lib/mongoid/persistence.rb:175:in `update_attributes'

      Mongoid blows up because it doesn't find a Favorite with the id contained in the nested attribute passed to update_attributes related to the person object.

      What do you think about mongoid:
      1. recognizing that the person object is not related to a Favorite with an id equal to the one contained in the nested attribute passed to update_attributes
      2. inserting the favorite's id in the person object's favorite_ids field

      I'm using mongoid 3.0.9, rails 3.2.8 and ruby 1.9.3p194.

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

              Created:
              Updated:
              Resolved: