Search code examples
ruby-on-railsruby-on-rails-3google-mapsgoogle-maps-api-3gmaps4rails

Google maps for rails: How to normalize two address fields


I am using the google maps for rails gem - https://github.com/apneadiving/Google-Maps-for-Rails. According to there model customization guide here - https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Model-Customization I would like to normalize my address field. But aparently I have two address (from and to). How do I do it?

I tried this

  acts_as_gmappable :normalized_address => "from_address" && "to_address"

  def gmaps4rails_address
    from_address && to_address
  end

It doesnt work entirely. Only to_address gets normalized. from_address stays as it is entered by the user (Although it calculates it fine. But I would like to have the normalized version).

Any help?


Solution

  • I think the problem is with the models rather than gmaps4rails. You should have each address as a polymorphic object. Then attach twice to the parent model; once as from_address and once as to_address. Each address can then be normalized on it's own.

    Edited with example....

    Example:

    class Address < ActiveRecord::Base
      belongs_to :mappable, :polymorphic => true
      acts_as_gmappable :normalized_address => "address_string"
    
      def gmaps4rails_address
         address_string
      end
    end
    
    class YourMainModel < ActiveRecord::Base
      has_one :from_address, :as => :mappable
      has_one :to_address, :as => :mappable
    end