I have a polymorphic Location model:
class Location < ActiveRecord::Base
开发者_运维知识库 acts_as_mappable
before_validation :geocode_address, :on => :create
belongs_to :locatable, :polymorphic => true
end
And a User model that references it:
class User < ActiveRecord::Base
acts_as_mappable :through => :location
has_one :location, :as => :locatable
end
What is the correct way of assigning the Location to the User in the Rails console? When I try the following, I get an error:
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.
I don't get a chance to assign the location to the user.
If I remove the 'acts_as_mappable :through => :location' instruction, I can assign a Location without a problem:
l = Location.create(:full_address=>'123 maple street, chicago, il')
u = User.create(:username=>'foo')
u.location = l
The definition of :location need to precede its usage:
class User < ActiveRecord::Base
has_one :location, :as => :locatable
acts_as_mappable :through => :location
end
精彩评论