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

Duplicated children using has_many and accepts_nested_attributes_for

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 6.1.1
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Have posted to StackOverflow, but been less than successful there with Mongoid questions ...

      I have something very odd going on with a (seemingly) fairly simple relationship - children are duplicated when validation fails on them - I have tried every combination of parameters and variations I can and am now out of ideas

      http://stackoverflow.com/questions/23122508/duplicated-has-many-records-using-accepts-nested-attribute

      Using Rails 4 and Mongoid 4 from master

      For some odd reason I am getting duplicated child records when a record fails validation (but not when it is valid) the form that then renders back to the user has the extra records in it

      I have a Pipeline, which has many PipelineStages

      Editing a Stage name to blank (which will fail validation) duplicates all the Pipeline.pipeline_stages

       ruby
      class Pipeline
        include Mongoid::Document
        has_many :pipeline_stages, dependent: :destroy
        accepts_nested_attributes_for :pipeline_stages, allow_destroy: true
      end
      
      class PipelineStage
        include Mongoid::Document
        belongs_to :pipeline
        validates :name, presence: true, length: { maximum:50 }
      end
      

      So when passed into the controller, what appear to be perfectly valid params don't work when update_attributes is used

       ruby
      [1] pry(#<PipelinesController>)> valid_params
      => {"name"=>"ddd",
       "pipeline_stages_attributes"=>
        {"0"=>
          {"id"=>"534f170e4a616b8368960000",
           "name"=>"dddd",
           "_destroy"=>"false",
           "sort_order"=>"9999"},
         "1"=>
          {"id"=>"534f1b554a616b8368a70000",
           "name"=>"",
           "_destroy"=>"false",
           "sort_order"=>"9999"}}}
      [2] pry(#<PipelinesController>)> @pipeline.pipeline_stages
      => [#<PipelineStage _id: 534f170e4a616b8368960000, created_at: 2014-04-16 23:49:34 UTC, updated_at: 2014-04-16 23:49:34 UTC, pipeline_id: BSON::ObjectId('534f170e4a616b8368950000'), name: "dddd", stage_number: nil, sort_order: 9999>, 
       #<PipelineStage _id: 534f1b554a616b8368a70000, created_at: 2014-04-17 00:07:49 UTC, updated_at: 2014-04-17 00:07:57 UTC, pipeline_id: BSON::ObjectId('534f170e4a616b8368950000'), name: "asdasdsadasd2", stage_number: nil, sort_order: 9999>]
      
      [3] pry(#<PipelinesController>)> @pipeline.update_attributes(valid_params)
      => false
      
      [4] pry(#<PipelinesController>)> @pipeline.pipeline_stages
      => [#<PipelineStage _id: 534f170e4a616b8368960000, created_at: 2014-04-16 23:49:34 UTC, updated_at: 2014-04-16 23:49:34 UTC, pipeline_id: BSON::ObjectId('534f170e4a616b8368950000'), name: "dddd", stage_number: nil, sort_order: 9999>, 
       #<PipelineStage _id: 534f1b554a616b8368a70000, created_at: 2014-04-17 00:07:49 UTC, updated_at: 2014-04-17 00:07:57 UTC, pipeline_id: BSON::ObjectId('534f170e4a616b8368950000'), name: "asdasdsadasd2", stage_number: nil, sort_order: 9999>, 
       #<PipelineStage _id: 534f170e4a616b8368960000, created_at: 2014-04-16 23:49:34 UTC, updated_at: 2014-04-16 23:49:34 UTC, pipeline_id: BSON::ObjectId('534f170e4a616b8368950000'), name: "dddd", stage_number: nil, sort_order: 9999>, 
       #<PipelineStage _id: 534f1b554a616b8368a70000, created_at: 2014-04-17 00:07:49 UTC, updated_at: 2014-04-17 00:07:57 UTC, pipeline_id: BSON::ObjectId('534f170e4a616b8368950000'), name: "", stage_number: nil, sort_order: 9999>]
      
      

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            jakcharlton jakcharlton
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: