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

Updating embedded document creates new field on parent document

    • Type: Icon: Task Task
    • Resolution: Done
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      I have a Mongoid model with embeds some address parts:

      class Address
      
        field :pao, type: String
        field :sao, type: String
        field :full_address, type: String
        field :full_address_lines, type: Array
      
        embeds_one :street
        embeds_one :locality
        embeds_one :town
        embeds_one :postcode
      
      end
      
      class Postcode
        
        include AddressPart
      
        field :area, type: String
        field :outcode, type: String
        field :incode, type: String
        field :easting, type: Integer
        field :northing, type: Integer
        field :introduced, type: Date
        field :terminated, type: Date
        field :authority, type: String
      
      end
      
      module AddressPart
      
        extend ActiveSupport::Concern
      
        included do
      
          include Tokenable
          include Mongoid::Geospatial
      
          validates_presence_of :name
      
          field :name, type: String
          field :lat_lng, type: Mongoid::Geospatial::Point
          field :easting_northing, type: Mongoid::Geospatial::Point
      
          index({ name: 1 })
          index({lat_lng: "2d"})
          index({easting_northing: "2d"}, {min: 0, max: 1300000})
        end
      end
      

      and am trying to make some changes to the postcode, so I run:

      a = Address.first
      # => #<Address _id: 547468bd537475e3dd000000, created_at: 2014-11-25 11:32:13 UTC, updated_at: 2015-03-19 08:56:37 UTC, token: "MLfuLn", provenance: {"activity"=>{"executed_at"=>2014-11-25 11:32:13 UTC, "processing_scripts"=>"https://github.com/OpenAddressesUK/distiller", "derived_from"=>[{"type"=>"Source", "urls"=>[nil], "downloaded_at"=>2014-11-25 11:32:13 UTC, "processing_script"=>"https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb"}, {"type"=>"Source", "urls"=>["http://alpha.openaddressesuk.org/postcodes/ayWW01"], "downloaded_at"=>2014-11-25 11:32:13 UTC, "processing_script"=>"https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb"}, {"type"=>"Source", "urls"=>["http://alpha.openaddressesuk.org/streets/PkaH9S"], "downloaded_at"=>2014-11-25 11:32:13 UTC, "processing_script"=>"https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb"}, {"type"=>"Source", "urls"=>["http://alpha.openaddressesuk.org/towns/4194LO"], "downloaded_at"=>2014-11-25 11:32:13 UTC, "processing_script"=>"https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb"}]}}, pao: "27", sao: nil, full_address: "27 OLD GLOUCESTER STREET, LONDON, WC1N 3AX", full_address_lines: ["27 OLD GLOUCESTER STREET", "LONDON", "WC1N 3AX"]>
      a.postcode.name = "ABC 123"
      #=> "ABC 123"
      a.save
      #=> true
      

      This all seems to work fine, until I reload the document:

      a = Address.first
      #=> #<Address _id: 547468bd537475e3dd000000, created_at: 2014-11-25 11:32:13 UTC, updated_at: 2015-03-19 09:15:29 UTC, token: "MLfuLn", provenance: {"activity"=>{"executed_at"=>2014-11-25 11:32:13 UTC, "processing_scripts"=>"https://github.com/OpenAddressesUK/distiller", "derived_from"=>[{"type"=>"Source", "urls"=>[nil], "downloaded_at"=>2014-11-25 11:32:13 UTC, "processing_script"=>"https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb"}, {"type"=>"Source", "urls"=>["http://alpha.openaddressesuk.org/postcodes/ayWW01"], "downloaded_at"=>2014-11-25 11:32:13 UTC, "processing_script"=>"https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb"}, {"type"=>"Source", "urls"=>["http://alpha.openaddressesuk.org/streets/PkaH9S"], "downloaded_at"=>2014-11-25 11:32:13 UTC, "processing_script"=>"https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb"}, {"type"=>"Source", "urls"=>["http://alpha.openaddressesuk.org/towns/4194LO"], "downloaded_at"=>2014-11-25 11:32:13 UTC, "processing_script"=>"https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb"}]}}, pao: "27", sao: nil, full_address: "27 OLD GLOUCESTER STREET, LONDON, ABC 123", full_address_lines: ["27 OLD GLOUCESTER STREET", "LONDON", "ABC 123"]>
      a.postcode.name
      #=> "WC1N 3AX"
      

      So, for some reason, the embedded document is not getting saved correctly.

      Looking at the raw MongoDB database, I load that document, and I can see that the postcode attribute is being saved against the parent document:

      db.addresses.find({"_id": ObjectId("547468bd537475e3dd000000")})
      { "_id" : ObjectId("547468bd537475e3dd000000"), "sao" : null, "pao" : "27", "provenance" : { "activity" : { "executed_at" : ISODate("2014-11-25T11:32:13.058Z"), "processing_scripts" : "https://github.com/OpenAddressesUK/distiller", "derived_from" : [ { "type" : "Source", "urls" : [ null ], "downloaded_at" : ISODate("2014-11-25T11:32:13.059Z"), "processing_script" : "https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb" }, { "type" : "Source", "urls" : [ "http://alpha.openaddressesuk.org/postcodes/ayWW01" ], "downloaded_at" : ISODate("2014-11-25T11:32:13.061Z"), "processing_script" : "https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb" }, { "type" : "Source", "urls" : [ "http://alpha.openaddressesuk.org/streets/PkaH9S" ], "downloaded_at" : ISODate("2014-11-25T11:32:13.061Z"), "processing_script" : "https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb" }, { "type" : "Source", "urls" : [ "http://alpha.openaddressesuk.org/towns/4194LO" ], "downloaded_at" : ISODate("2014-11-25T11:32:13.061Z"), "processing_script" : "https://github.com/OpenAddressesUK/distiller/tree/cb5ee54460026b95682af84798f6b288723e69a4/lib/distil.rb" } ] } }, "full_address_lines" : [ "27 OLD GLOUCESTER STREET", "LONDON", "ABC 123" ], "full_address" : "27 OLD GLOUCESTER STREET, LONDON, ABC 123", "token" : "MLfuLn", "updated_at" : ISODate("2015-03-19T09:15:29.936Z"), "created_at" : ISODate("2014-11-25T11:32:13.099Z"), "street" : { "_id" : ObjectId("546f87f553747596c8279f30"), "name" : "OLD GLOUCESTER STREET", "settlement" : "Camden", "locality" : "Holborn and Covent Garden", "authority" : "Camden London Borough", "lat_lng" : [ 51.52012901770905, -0.12136457809717259 ], "easting_northing" : [ 181807, 530438 ], "provenance" : { "activity" : { "executed_at" : ISODate("2014-11-21T18:44:05.418Z"), "processing_scripts" : "https://github.com/OpenAddressesUK/distiller", "derived_from" : [ { "name" : "OS Locator", "type" : "Source", "urls" : [ "https://github.com/OpenAddressesUK/OS_Locator/blob/gh-pages/OS_Locator2014_2_OPEN_xac.txt?raw=true" ], "downloaded_at" : ISODate("2014-11-21T18:41:38.045Z"), "description_url" : "http://www.ordnancesurvey.co.uk/business-and-government/products/os-locator.html", "processing_script" : "https://github.com/OpenAddressesUK/distiller/tree/63c86faca8318e1a575dc444fa01459803d2ef5c/lib/import.rb" } ] } }, "token" : "PkaH9S", "updated_at" : ISODate("2014-11-21T18:44:05.419Z"), "created_at" : ISODate("2014-11-21T18:44:05.419Z") }, "town" : { "_id" : ObjectId("546f6e2f53747596c8870100"), "area" : "E", "name" : "LONDON", "provenance" : { "activity" : { "executed_at" : ISODate("2014-11-21T16:54:07.899Z"), "processing_scripts" : "https://github.com/OpenAddressesUK/distiller", "derived_from" : [ { "name" : "Wikipedia list of Post Towns in the United Kingdom", "type" : "Source", "urls" : [ "https://en.wikipedia.org/wiki/List_of_post_towns_in_the_United_Kingdom" ], "downloaded_at" : ISODate("2014-11-21T16:54:06.458Z"), "description_url" : "https://en.wikipedia.org/wiki/List_of_post_towns_in_the_United_Kingdom", "processing_script" : "https://github.com/OpenAddressesUK/distiller/tree/63c86faca8318e1a575dc444fa01459803d2ef5c/lib/import.rb" } ] } }, "token" : "4194LO", "updated_at" : ISODate("2014-11-21T16:54:07.900Z"), "created_at" : ISODate("2014-11-21T16:54:07.900Z") }, "postcode" : { "_id" : ObjectId("546f82f353747596c8e83925"), "name" : "WC1N 3AX", "area" : "WC", "outcode" : "WC1N", "incode" : "3AX", "easting" : 530387, "northing" : 181874, "introduced" : ISODate("2007-09-01T00:00:00Z"), "terminated" : null, "authority" : "E09000007", "lat_lng" : [ 51.52074288509297, -0.12207445241194696 ], "easting_northing" : [ 181874, 530387 ], "provenance" : { "activity" : { "executed_at" : ISODate("2014-11-21T18:22:43.355Z"), "processing_scripts" : "https://github.com/OpenAddressesUK/distiller", "derived_from" : [ { "name" : "ONS Postcode Directory (UK) Aug 2014", "type" : "Source", "urls" : [ "https://geoportal.statistics.gov.uk/Docs/PostCodes/ONSPD_AUG_2014_csv.zip" ], "downloaded_at" : ISODate("2014-11-21T16:54:10.410Z"), "description_url" : "https://geoportal.statistics.gov.uk/geoportal/catalog/search/resource/details.page?uuid=%7B473A5770-FB1B-4C1A-AEEC-5DC056E5EC7F%7D", "processing_script" : "https://github.com/OpenAddressesUK/distiller/tree/63c86faca8318e1a575dc444fa01459803d2ef5c/lib/import.rb" } ] } }, "token" : "ayWW01", "updated_at" : ISODate("2014-11-21T18:22:43.356Z"), "created_at" : ISODate("2014-11-21T18:22:43.356Z") }, "name" : "ABC 123" }
      

      Is this a bug, or am I doing something wrong? I'm using Mongoid version 4.0.0 and MongoDB version 2.6.7.

            Assignee:
            Unassigned Unassigned
            Reporter:
            pezholio pezholio
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: