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

update_attributes updates parent document instead of embedded doc with embeds_one and custom relation name

      I'm having an issue where update_attributes of a nested embeds_one relationship updates fields on the parent model instead of the child. This happens whether or not the "id" key is present in the home_team_attributes hash. There is no attr_accessible control on the GameTeam model.

      I'm running mongoid 3.0.6, rails 3.1.3

      snipped game model

      class Game
      include Mongoid::Document

      embeds_one :home_team, :class_name => GameTeam.to_s
      embeds_one :away_team, :class_name => GameTeam.to_s

      accepts_nested_attributes_for :home_team, :away_team
      attr_accessible :home_team_attributes, :away_team_attributes

      ...
      end

      test cases that reproduce the error:

      context "when mass assigning home team name" do
      it "should not update game name which isn't even a mongoid field" do
      expect {
      @game.update_attributes!( home_team_attributes:

      { name: "poopy" }

      )
      @game.reload
      }.not_to change

      { @game.attributes['name'] }

      end

      it "should allow updating home team name" do
      expect {
      @game.update_attributes!( home_team_attributes:

      { name: "poopy" }

      )
      @game.reload
      }.to change

      { @game.home_team.name }.to "poopy"
      end

      it "should allow updating home team name directly" do
      expect { @game.home_team.update_attributes!( name: "poopy" ) @game.reload }.to change { @game.home_team.name }

      .to "poopy"
      end
      end

      rspec output:

      Testing started at 2:20 PM ...

      result should not have changed, but did change from nil to "poopy"
      [[snip]]

      result should have been changed to "poopy", but is now "Florida Gators"
      [[snip]]

      result should have been changed to "poopy", but is now "Florida Gators"
      [[snip]]

      3 examples, 3 failures, 0 passed

      Finished in 0.156565 seconds

      Process finished with exit code 1

            Assignee:
            durran Durran Jordan
            Reporter:
            jcihocki jcihocki
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: