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

Bug in accept_nested_attributes with two levels deep

    • Type: Icon: Task Task
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • None

      Configuration

      • gem 'rails', '3.2.13'
      • gem 'mongoid', :git => 'git://github.com/mongoid/mongoid.git', :branch => '3.1.0-stable'

      Test case

      Please notice the FIXMEs in rspec comments

      class SubComponent
        include Mongoid::Document
        field :name, type: String
        embeds_many :group_profiles
        accepts_nested_attributes_for :group_profiles, :allow_destroy => true
      end
      
      class GroupProfile
        include Mongoid::Document
        field :name, type: String
        embedded_in :sub_component
        embeds_many :traveler_profiles
        accepts_nested_attributes_for :traveler_profiles, :allow_destroy => true
      end
      
      class TravelerProfile
        include Mongoid::Document
        field :name, type: String
        embedded_in :group_profile
      end
      
      require 'spec_helper'
      
      describe "groups_profiles" do
        it "handle traveler attributes" do
          sc = SubComponent.create(name: "Testing",
                                   "group_profiles_attributes" => {
                                       "0" => {"_destroy" => "", "name" => "X",
                                               "traveler_profiles_attributes" => {
                                                   "0" => {"name" => "", "_destroy" => ""},
                                                   "1" => {"name" => "", "_destroy" => ""},
                                                   "2" => {"name" => "", "_destroy" => ""}
                                               }},
                                       "1" => {"_destroy" => "", "name" => "",
                                               "traveler_profiles_attributes" => {
                                                   "0" => {"name" => "", "_destroy" => ""}
                                               }},
                                       "2" => {"_destroy" => "", "name" => "",
                                               "traveler_profiles_attributes" => {
                                                   "0" => {"name" => "", "_destroy" => ""}
                                               }}
                                   })
          sc.should be_persisted
          sc.reload.group_profiles.count.should eq(3)
      
          gp1 = sc.group_profiles.to_a[0]
          gp2 = sc.group_profiles.to_a[1]
          gp3 = sc.group_profiles.to_a[2]
      
          gp1.traveler_profiles.count.should eq(3)
          gp2.traveler_profiles.count.should eq(1)
          gp3.traveler_profiles.count.should eq(1)
      
      
          tp_1_1 = gp1.traveler_profiles.to_a[0]
          tp_1_2 = gp1.traveler_profiles.to_a[1]
          tp_1_3 = gp1.traveler_profiles.to_a[2]
      
          tp_2_1 = gp2.traveler_profiles.to_a[0]
          tp_3_1 = gp3.traveler_profiles.to_a[0]
      
          group_attributes = ({"group_profiles_attributes" => {
              "0" => {"_destroy" => "", "name" => "",
                      "traveler_profiles_attributes" => {
                          "0" => {"name" => "", "_destroy" => "", "id" => tp_1_1.id},
                          "1" => {"name" => "", "_destroy" => "", "id" => tp_1_2.id},
                          "2" => {"name" => "", "_destroy" => "1", "id" => tp_1_3.id} #FIXME: If  "_destroy" => "", then the test passes!
                      }, "id" => gp1.id},
              "1" => {"_destroy" => "", "name" => "",
                      "traveler_profiles_attributes" => {
                          "0" => {"name" => "", "_destroy" => "", "id" => tp_2_1.id}
                      }, "id" => gp2.id},
              "2" => {"_destroy" => "1", "name" => "",
                      "traveler_profiles_attributes" => {
                          "0" => {"name" => "", "_destroy" => "", "id" => tp_3_1.id}
                      }, "id" => gp3.id}
          }})
      
          sc.update_attributes(group_attributes)
      
          sc.reload.group_profiles.count.should eq(2) #FIXME: But is 3!
        end
      end
      

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

              Created:
              Updated:
              Resolved: