2011-02-10 32 views
1

我有一个多态位点模式:分配在Rails的多态acts_as_mappable模型控制台

class Location < ActiveRecord::Base 
    acts_as_mappable 
    before_validation :geocode_address, :on => :create 
    belongs_to :locatable, :polymorphic => true 
end 

引用它一个用户模式:

class User < ActiveRecord::Base 
    acts_as_mappable :through => :location 
    has_one :location, :as => :locatable 
end 

什么是分配位置的正确方法到Rails控制台中的用户?当我尝试以下时,出现错误:

l = Location.create(:full_address=>'123 maple street, chicago, il') 
u = User.create(:username=>'foo') # => ArgumentError: You gave location in :through, but I could not find it on User. 

我没有机会将位置分配给用户。

如果删除了“acts_as_mappable:通过=>:位置”指令,我可以分配一个位置没有问题:

l = Location.create(:full_address=>'123 maple street, chicago, il') 
u = User.create(:username=>'foo') 
u.location = l 

回答

1

的定义:位置需要先于它的使用:

class User < ActiveRecord::Base 
    has_one :location, :as => :locatable 
    acts_as_mappable :through => :location 
end