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

没有正确的解决方案

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top