Pregunta

I built an app that creates a user model and automatically creates a profile model and a location(called living) model using the after_create callback method in the model. The model profile belongs to users, and users has one profile, and the model location belong to profile and profile has one location.

The model called location, use the gem geocoder with the current_ip address of the client machine, to pull out the current location of the user, and then save the address, the latitude and longitude in the same model.

Everything works perfectly and fine, except, when I try to add to the location model, acts_as_gmappable, then during the registration process, the same model is not more automatically created, and this prevent the creation of the database column address and obviously consequently, this will bring to an error of gmapsforails.

How I can use the after_create callback in the profile model, and then automatically create the location model, and its column (longitude, latitude, address, ip_address) and at the same time using after the acts_as_gmappable?

I think it's a problem related with which method is called before: the after_create or the gmapsforrails one...this because, if I cut from my location model the call to acts_as_gmappable, register the user, and after past back in the location model the acts_as_gmappable, everything works fine, and the google map is showed.

Thanks to everyone that could in someway highlight my mind...

(1) THE PROFILE MODEL

class Profile < ActiveRecord::Base

  # Setup model relationships (has_many require plural)
  belongs_to :user
  has_one :living

  after_create :create_living

  accepts_nested_attributes_for :living

  # Setup accessible (or protected) attributes for your model
  attr_accessible :registration_ip, :registration_timezone, :registration_address, :registration_address_components, :latitude, :longitude, :living_attributes

  # Setup Geocoder routine
  geocoded_by :registration_ip
  after_validation :geocode

  reverse_geocoded_by :latitude, :longitude do |obj,results|
    if geo = results.first
      obj.registration_address_components  = ActiveSupport::JSON.encode(geo.address_components)
      obj.registration_address  = geo.address
    end
  end

  after_validation :reverse_geocode

end

(2) THE LIVING MODEL

class Living < ActiveRecord::Base

  # Setup model relationships
  belongs_to :profile

  # Setup accessible (or protected) attributes for your model
  attr_accessible :current_ip, :current_timezone, :current_address, :current_address_components,     :latitude, :longitude, :gmaps

  geocoded_by :current_ip
  after_validation :geocode, :if => :current_ip_changed?

  reverse_geocoded_by :latitude, :longitude do |obj,results|
    if geo = results.first
      obj.current_address_components  = ActiveSupport::JSON.encode(geo.address_components)
      obj.current_address  = geo.address
    end
  end

  after_validation :reverse_geocode, :if => :current_ip_changed?


  acts_as_gmappable :lat => 'glat', :lng => 'glng', :check_process => :prevent_geocoding,
                    :address => "current_address", :normalized_address => "current_address",
                    :msg => "Sorry, not even Google could figure out where that is"

  def prevent_geocoding
    current_address.blank? || (!latitude.blank? && !longitude.blank?)
  end
end

NOTE The create_before for the profile is in the user model. With this system, everytime that an user register, automatically the app create an user record in the user table, a profile record in the profile table and a living record in the living record. Everything works perfectly smooth except when I add the acts_as_gmappable in the living model. At that point, the record in the living model table is not more automatically created, and obviously the google maps will not work because will not find the address in the table of the living model (the living model will be nil).

I was thinking that maybe due to the before_create method used in the model, to add a map to living, i will need to create another model let me say gmapliving that belong to living, but at that point I don't have idea how i could ever access the record saved for the address in the living model table.

Please some suggestions?

Thanks Dinuz

¿Fue útil?

Solución

Because you don't need geocoding from gmaps4rails, add this option:

:process_geocoding => false

Everything is explained here: https://github.com/apneadiving/Google-Maps-for-Rails/wiki/Model-Customization

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top