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

Duplicate insert on child - 1-N Reference

      I'm experiencing an error, identical to https://github.com/mongoid/mongoid/issues/1516 . But this time I'm running Mongoid 3.0.6 on Rails 3.2.8.

      When I try to add a child node on safe mode (already tried every way present on http://mongoid.org/en/mongoid/docs/relations.html#has_many), the insert is performed twice, producing the following error:

      Mongo::OperationFailure: 11000: E11000 duplicate key error
      

      Has anyone experienced the same?

      Model Specs:

      # Models
      ## user.rb
      class User
        include Mongoid::Document
        include Mongoid::Timestamps
      
        has_many :homes, dependent: :destroy
      
      
      ## home.rb
      class Home
        include Mongoid::Document
        include Mongoid::Timestamps
        include Geocoder::Model::Mongoid
      
        belongs_to :user
      
      # Controller
      ## homes_controller.rb
      
        def create
            current_user.homes.create!(params[:home])
      
      ### On the creation of a Home instance, Mongoid performs the insert twice, producing this error:
      
      #   MOPED: 127.0.0.1:27017 INSERT       database=unplugg_development collection=homes documents=[{"_id"=>"50687110c0d1c57287000001", "monthly_avg"=>0, "weekly_avg"=>0, "daily_avg"=>0, "spent"=>0, "expected"=>0, "trend_ever"=>0, "trend_month"=>0, "month_comparison"=>0, "week_comparison"=>0, "today_yesterday_comparison"=>0, "today_equal_weekday_comparison"=>0, "monthly_avg_price"=>0.0, "weekly_avg_price"=>0.0, "daily_avg_price"=>0.0, "spent_price"=>0.0, "expected_price"=>0.0, "people"=>1, "address"=>"Coimbra, Portugal", "typo"=>"T2", "has_ac"=>false, "has_heater"=>false, "user_id"=>"4fa19b80e99f633fd9000004", "coordinates"=>[-8.414617699999999, 40.2065687], "ted_token"=>"kiyHkHbFxvQXYCaokdAsIsWDEDFUTDJqTajTwwmjSzVMAeZjOsb", "updated_at"=>2012-09-30 16:19:28 UTC, "created_at"=>2012-09-30 16:19:28 UTC}] flags=[]
      #                          COMMAND      database=unplugg_development command={:getlasterror=>1, :safe=>true} (0.9232ms)
      #   MOPED: 127.0.0.1:27017 INSERT       database=unplugg_development collection=homes documents=[{"_id"=>"50687110c0d1c57287000001", "monthly_avg"=>0, "weekly_avg"=>0, "daily_avg"=>0, "spent"=>0, "expected"=>0, "trend_ever"=>0, "trend_month"=>0, "month_comparison"=>0, "week_comparison"=>0, "today_yesterday_comparison"=>0, "today_equal_weekday_comparison"=>0, "monthly_avg_price"=>0.0, "weekly_avg_price"=>0.0, "daily_avg_price"=>0.0, "spent_price"=>0.0, "expected_price"=>0.0, "people"=>1, "address"=>"Coimbra, Portugal", "typo"=>"T2", "has_ac"=>false, "has_heater"=>false, "user_id"=>"4fa19b80e99f633fd9000004", "coordinates"=>[-8.414617699999999, 40.2065687], "ted_token"=>"kiyHkHbFxvQXYCaokdAsIsWDEDFUTDJqTajTwwmjSzVMAeZjOsb", "updated_at"=>2012-09-30 16:19:28 UTC, "created_at"=>2012-09-30 16:19:28 UTC}] flags=[]
      #                          COMMAND      database=unplugg_development command={:getlasterror=>1, :safe=>true} (6.0902ms)
      # Completed 500 Internal Server Error in 200ms
      # 
      # Moped::Errors::OperationFailure (The operation: #<Moped::Protocol::Command
      #   @length=83
      #   @request_id=44
      #   @response_to=0
      #   @op_code=2004
      #   @flags=[]
      #   @full_collection_name="unplugg_development.$cmd"
      #   @skip=0
      #   @limit=-1
      #   @selector={:getlasterror=>1, :safe=>true}
      #   @fields=nil>
      # failed with error 11000: "E11000 duplicate key error index: unplugg_development.homes.$_id_  dup key: { : ObjectId('50687110c0d1c57287000001') }"
      # 
      # See https://github.com/mongodb/mongo/blob/master/docs/errors.md
      # for details about this error.):
      #   app/controllers/homes_controller.rb:72:in `create'
      

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

              Created:
              Updated:
              Resolved: