2012-03-08 77 views
0

我构建了一个应用程序,该应用程序创建用户模型并使用模型中的after_create回调方法自动创建配置文件模型和位置(称为生活)模型。模型配置文件属于用户,用户有一个配置文件,模型位置属于配置文件,配置文件有一个位置。Rails 3:After_create问题与acts_as_gmappable

该模型称为位置,使用gem地理编码器和客户端机器的current_ip地址来拉出用户的当前位置,然后将地址,经度和纬度保存在同一模型中。

一切工作完美和罚款,除了当我尝试添加到位置模型acts_as_gmappable,然后在注册过程中,相同的模型不会自动创建,并且这阻止创建数据库列地址和很显然,这会导致gmapsforails的错误。

如何在配置文件模型中使用after_create回调,然后自动创建位置模型及其列(longitude,latitude,address,ip_address)并同时使用acts_as_gmappable之后?

我认为这是一个与之前调用哪种方法有关的问题:after_create或gmapsforrails之一...这是因为,如果我从位置模型中调用acts_as_gmappable,注册用户,并在过去回到位置模型acts_as_gmappable,一切工作正常,并显示谷歌地图。

感谢大家可能会以某种方式强调我的脑海里......

(1)剖面模型

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)居住模式

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 

注意的create_before该配置文件位于用户模型中。 使用此系统,每次用户注册时,应用程序都会自动在用户表中创建用户记录,在配置文件表中创建配置文件记录,并在活动记录中创建活动记录。 除非在生活模型中添加acts_as_gmappable,否则一切都可以完美平滑。此时,生活模式表中的记录不会自动创建,显然谷歌地图不起作用,因为无法在生存模型表中找到地址(生存模型将为零)。

我在想,也许是由于在模型中使用的before_create方法,要添加一张地图到生活中,我需要创建另一个模型让我说gmapliving属于生活,但在那一点上,我不有想法我怎么能访问保存的生存模型表中的地址记录。

请给点建议?

感谢 Dinuz

+0

这是一个有点混乱,阅读,我猜所有信息都没有必要。我猜'gmaps4rails'试图无法成功地进行地理编码。你有没有尝试做:'def prevent_geocoding; new_record? || current_address.blank? || (!latitude.blank? &&!longitude.blank?); 结束' – apneadiving 2012-03-08 22:18:42

+0

为什么你需要gmaps4rails地理编码,因为你使用geocoder? – apneadiving 2012-03-08 22:20:32

+0

我想要地图......就这些!我想在同一张地图上显示注册位置和当前位置。我不想使用gmaps4rails进行地理编码,而是想使用它可以显示的地图。 – Dinuz 2012-03-11 20:21:41

回答