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

Issues with Geocoder and Mongoid 3.0.0rc not sure which one is causing the issue

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.0
    • Affects Version/s: None
    • Component/s: None
    • None

      Issue:

      I am using the most up to date Mongoid, Moped and Geocoder ( with a issue dealing with the new Mongoid index syntax fixed )

      I am using formatastic also to create the simple form

      Here is my Models and Forms I will continue below the form

      My Account Model

      class Account
        include Mongoid::Document
        include Mongoid::Timestamps
        
        ## fields ##
        field :subdomain, type: String
        field :time_zone, type: String, default: 'Mountain Time (US & Canada)'
        
        ## associations ##
        embeds_one :location, as: :locatable, :cascade_callbacks => true, :autobuild => true
      
        attr_accessible :location_attributes
      
        accepts_nested_attributes_for :location
      
      end
      

      My Location Model

      class Location
        include Mongoid::Document
      
        ## fields ##
        field :coordinates, type: Array
        field :address, type: String
        field :address_formatted, type: String
        field :address_1, type: String
        field :address_2, type: String
        field :latitude, type: Float
        field :longitude, type: Float
        field :city, type: String
        field :county, type: String
        field :county_code, type: String
        field :state, type: String
        field :state_code, type: String
        field :zip, type: String
        field :country, type: String
        field :country_code, type: String
        
        ## associations ##
        belongs_to :locatable, polymorphic: true
        
        attr_accessible :coordinates, :address, :address_formatted, 
             :address_1, :address_2, :latitude, :longitude, :city, 
             :county, :address_1, :county_code, :state, :state_code, 
              :zip, :country, :country_code
        
        ## callbacks ##
        include Geocoder::Model::Mongoid
        geocoded_by :address_info
        after_validation :geocode
        
        ## methods ##
        geocoded_by :address_info do |obj,results|
          
          # raise obj.to_json
          if geo = results.first
            
            obj.latitude     = geo.latitude
            obj.longitude    = geo.longitude
            obj.coordinates  = geo.coordinates
            obj.city         = geo.city
            
            address_info_1 = geo.address_components_of_type(:street_number).first
            address_info_2 = geo.address_components_of_type(:route).first
            
            if address_info_1
              obj.address_1 = address_info_1['long_name'] 
              obj.address_1 += " #{address_info_2['long_name']}" if address_info_2
              obj.address = "#{obj.address_1}"
              obj.address += " #{obj.address_2}" if obj.address_2
            end
            
            if county = geo.address_components_of_type(:administrative_area_level_2).first
              obj.county = county['long_name']
            end
            
            if county = geo.address_components_of_type(:administrative_area_level_2).first
              obj.county_code = county['short_name']
            end
            
            obj.state        = geo.state
            obj.state_code   = geo.state_code
            obj.zip          = geo.postal_code
            obj.country      = geo.country
            obj.country_code = geo.country_code
            obj.address_formatted = geo.address.gsub(', USA', '')
          end
        end
      
        def address_info
          [address_1, address_2, city, state, zip].compact.join(' ')
        end
      end
      

      My form

      = semantic_form_for [:dashboard, current_account], :url => 'settings' do |f|
        = f.inputs do
          = f.input :subdomain
      
          = f.semantic_fields_for :location do |location|
            = location.input :address_1
            = location.input :address_2
            = location.input :city
            = location.input :state, :collection => us_states, :include_blank => false
            = location.input :zip
      
        = f.actions do
          = f.action :submit, :as => :input
      

      Issue Continued

      Ok adding the location to the account works flawlessly below is the output during the creation of the location field that is embedded within the account

      Creating the location field within the account DB

      MOPED: localhost:27017 QUERY        database=resocialbuilder_development collection=accounts selector={
      "_id"=>{"$in"=>[4fe0ce819a6f23afbe000028]}} flags=[:slave_ok] limit=0 skip=0 fields=nil (0.0ms)
      
      MOPED: localhost:27017 UPDATE       database=resocialbuilder_development collection=accounts selector=
      {"_id"=>4fe0ce819a6f23afbe000028} update={"$set"=>{"updated_at"=>2012-06-22 07:39:23 UTC, "location"=>
      {"_id"=>4fe4212b9a6f234a0a000005, "address_1"=>"1211 W 540 N", "address_2"=>"B3", "city"=>"St George", 
      "state"=>"Utah", "zip"=>"84770", "latitude"=>37.11685, "longitude"=>-113.609706, 
      "coordinates"=>[37.11685, -113.609706], "address"=>"1211 W 540 N B3", "county"=>"Washington", 
      "county_code"=>"Washington", "state_code"=>"UT", "country"=>"United States", "country_code"=>"US", 
      "address_formatted"=>"1211 W 540 N, St George, UT 84770"}}} flags=[] (0.0ms)
      

      Parameters during the creation of location field within the account DB

      Parameters: {"utf8"=>"✓", "authenticity_token"=>"2/ZeJrzV6feQuOb+PY7HyB/uMLe/7oRJCupquImnRl8=", 
      "account"=>{"subdomain"=>"demo", "location_attributes"=>{"address_1"=>"1211 W 540 N", 
      "address_2"=>"B3", "city"=>"St. George", "state"=>"UT", "zip"=>"84770"}}, "commit"=>"Update Account"}
      

      Output directly after everything is saved to show what is coming back

      RuntimeError ({"_id":"4fe0ce819a6f23afbe000028","created_at":"2012-06-19T19:09:53Z",
      "location":{"_id":"4fe4212b9a6f234a0a000005","address":"1211 W 540 N B3",
      "address_1":"1211 W 540 N","address_2":"B3","address_formatted":"1211 W 540 N, St George, UT 84770",
      "city":"St George","coordinates":[37.11685,-113.609706],
      "country":"United States",
      "country_code":"US","county":"Washington","county_code":"Washington","latitude":37.11685,
      "locatable_field":null,"locatable_id":null,"locatable_type":null,"longitude":-113.609706,"state":
      "Utah","state_code":"UT","zip":"84770"},"subdomain":"demo","time_zone":"Mountain Time (US & Canada)",
      "updated_at":"2012-06-22T07:39:23Z"}):
      

      This is a snapshot of the database ( Everything looks like its working! )

              
      {
        "created_at": ISODate("2012-06-19T19:09:53.174Z"),
        "location": {
          "_id": ObjectId("4fe4212b9a6f234a0a000005"),
          "address_1": "1211 W 540 N",
          "address_2": "B3",
          "city": "St George",
          "state": "Utah",
          "zip": "84770",
          "latitude": 37.11685,
          "longitude": -113.609706,
          "coordinates": [
            37.11685,
            -113.609706
          ],
          "address": "1211 W 540 N B3",
          "county": "Washington",
          "county_code": "Washington",
          "state_code": "UT",
          "country": "United States",
          "country_code": "US",
          "address_formatted": "1211 W 540 N, St George, UT 84770"
        },
        "subdomain": "demo",
        "time_zone": "Mountain Time (US & Canada)",
        "updated_at": ISODate("2012-06-22T07:39:23.220Z")
      }
      

      Now the problem... Updating that same form. I am using the same form it is correctly populating the form with the last address. I am going to give it a new address....

      Updating the location field within the account DB

      MOPED: localhost:27017 COMMAND      database=admin command={:ismaster=>1} (0.0ms)
        
      MOPED: localhost:27017 QUERY        database=resocialbuilder_development collection=
      accounts selector={"_id"=>{"$in"=>[4fe0ce819a6f23afbe000028]}} flags=[:slave_ok] limit=0 
      skip=0 fields=nil (0.0ms)
        
      MOPED: localhost:27017 UPDATE       database=resocialbuilder_development collection=
      accounts selector={"_id"=>4fe0ce819a6f23afbe000028} update={"$set"=>{
      "updated_at"=>2012-06-22 07:44:14 UTC, "address_1"=>"590 Valley View Dr", "address_2"=>"", 
      "city"=>"Tooele", "zip"=>"84074", "latitude"=>40.54336, "longitude"=>-112.283402, 
      "coordinates"=>[40.54336, -112.283402], "address"=>"590 Valley View Dr ", 
      "county"=>"Tooele", "county_code"=>"Tooele", 
      "address_formatted"=>"590 Valley View Dr, Tooele, UT 84074"}} flags=[] (0.0ms)
      
      

      Parameters during the UPDATING of location field within the account DB

      Parameters: {"utf8"=>"✓", "authenticity_token"=>"2/ZeJrzV6feQuOb+PY7HyB/uMLe/7oRJCupquImnRl8=", 
      "account"=>{"subdomain"=>"demo", "location_attributes"=>{"address_1"=>"590 Valley View Drive", 
      "address_2"=>"", "city"=>"Tooele", "state"=>"UT", "zip"=>"84074", "id"=>"4fe4212b9a6f234a0a000005"}}, 
      "commit"=>"Update Account"}
      
      

      Output directly after everything is UPDATED to show what is coming back

      RuntimeError ({"_id":"4fe0ce819a6f23afbe000028","created_at":"2012-06-19T19:09:53Z",
      "location":{"_id":"4fe4212b9a6f234a0a000005","address":"590 Valley View Dr ",
      "address_1":"590 Valley View Dr","address_2":"","address_formatted":"590 Valley View Dr, Tooele, UT 84074",
      "city":"Tooele","coordinates":[40.54336,-112.283402],"country":"United States","country_code":"US",
      "county":"Tooele","county_code":"Tooele","latitude":40.54336,"locatable_field":null,
      "locatable_id":null,"locatable_type":null,"longitude":-112.283402,"state":"Utah",
      "state_code":"UT","zip":"84074"},"subdomain":"demo","time_zone":"Mountain Time (US & Canada)",
      "updated_at":"2012-06-22T07:44:14Z"}):
      

      Now this output directly after its saved looks like it worked perfectly this is what I dont get because now take a look at the snapshot of the database schema now....

      This is a snapshot of the database ( Everything looks like its working! )

         
      {
        "address": "590 Valley View Dr ",
        "address_formatted": "590 Valley View Dr, Tooele, UT 84074",
        "address_1": "590 Valley View Dr",
        "address_2": "",
        "city": "Tooele",
        "coordinates": [
          40.54336,
          -112.283402
        ],
        "county": "Tooele",
        "county_code": "Tooele",
        "created_at": ISODate("2012-06-19T19:09:53.174Z"),
        "latitude": 40.54336,
        "location": {
          "_id": ObjectId("4fe4212b9a6f234a0a000005"),
          "address_1": "1211 W 540 N",
          "address_2": "B3",
          "city": "St George",
          "state": "Utah",
          "zip": "84770",
          "latitude": 37.11685,
          "longitude": -113.609706,
          "coordinates": [
            37.11685,
            -113.609706
          ],
          "address": "1211 W 540 N B3",
          "county": "Washington",
          "county_code": "Washington",
          "state_code": "UT",
          "country": "United States",
          "country_code": "US",
          "address_formatted": "1211 W 540 N, St George, UT 84770"
        },
        "longitude": -112.283402,
        "subdomain": "demo",
        "time_zone": "Mountain Time (US & Canada)",
        "updated_at": ISODate("2012-06-22T07:44:14.530Z"),
        "zip": "84074"
      }
      

      As you can see its tacking on the new address on and leaving the old address within the location field but throwing the new address into the main Account, rather then embedding it in the location field as it should. Whats odd is the last output shows it works but the Moped outputs dont match it. Anyone have any idea why this is working like this

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

              Created:
              Updated:
              Resolved: