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

accepts_nested_attributes allows embedded documents to be updated even if parent is not valid

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

      Consider this working scenario:

      class Trip
        field :name, type: String
      
        embeds_many :destinations
        accepts_nested_attributes_for :destinations, allow_destroy: true
      end
      
      class Destination
        field :start, type: Date
        field :finish, type: Date
       
        embedded_in :trip
        belongs_to :country
      end
      

      Here is an example PUT request:

      Parameters: {"utf8"=>"✓", "authenticity_token"=>"nuQ0Tn04uqgb4GsohVMcuD7ZTSqj3DSN76UvEMUC2BU=", "trip"=>{"name"=>"My Trip to Bahamas", "destinations_attributes"=>{"0"=>{"country_id"=>"4f831e85fe75bd2c230004ee", "start"=>"2013-01-29", "finish"=>"2013-01-30", "_destroy"=>"false", "id"=>"51046b6ffe75bd4e5e000013"}, "1"=>{"country_id"=>"4f831e85fe75bd2c230004e7", "start"=>"2014-01-01", "finish"=>"2014-01-04", "_destroy"=>"false", "id"=>"51046ce3fe75bd4e5e000019"}}}, "commit"=>"Update Trip", "id"=>"my-trip-to-bahamas"}
      
      MOPED: 127.0.0.1:27017 QUERY        database=development collection=trips selector={"$query"=>{"_slugs"=>{"$in"=>["my-trip-to-bahamas"]}, "$and"=>[{"user_id"=>"51046b6ffe75bd4e5e000012"}]}, "$orderby"=>{"start"=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil (0.6957ms)
        MOPED: 127.0.0.1:27017 QUERY        database=development collection=trips selector={"$query"=>{"user_id"=>"51046b6ffe75bd4e5e000012", "_id"=>"51046b6ffe75bd4e5e000011"}, "$orderby"=>{"start"=>1}} flags=[] limit=1 skip=0 batch_size=nil fields={:_id=>1} (0.6163ms)
        MOPED: 127.0.0.1:27017 UPDATE       database=development collection=trips selector={"_id"=>"51046b6ffe75bd4e5e000011"} update={"$set"=>{"destinations"=>[{"_id"=>"51046b6ffe75bd4e5e000013", "country_id"=>"4f831e85fe75bd2c230004ee", "start"=>2013-01-29 00:00:00 UTC, "finish"=>2013-01-30 00:00:00 UTC}, {"_id"=>"51046ce3fe75bd4e5e000019", "country_id"=>"4f831e85fe75bd2c230004e7", "start"=>2014-01-01 00:00:00 UTC, "finish"=>2014-01-04 00:00:00 UTC}]}} flags=[] (0.1597ms)
        MOPED: 127.0.0.1:27017 QUERY        database=development collection=trips selector={"$query"=>{"user_id"=>"51046b6ffe75bd4e5e000012", "_id"=>{"$nin"=>["51046b6ffe75bd4e5e000011"]}}, "$orderby"=>{"start"=>1}} flags=[] limit=0 skip=0 batch_size=nil fields=nil (0.6883ms)
      Completed 422 Unprocessable Entity in 4040ms (Views: 0.4ms)
      

      Note the UPDATE despite the 422.

      This is an undesired outcome since validations on the Trip might want to forgo all persistence. For example, a validation that loops through all Destinations to ensure they occur consecutively with no time gaps between them.

      As of Mongoid 3.0.19. Any help is appreciated. Thanks.

            Assignee:
            Unassigned Unassigned
            Reporter:
            astjohn Adam St. John
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: