-
Type:
Bug
-
Resolution: Done
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
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
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>]