开发者

Assigning a polymorphic acts_as_mappable model in the Rails console

开发者 https://www.devze.com 2023-02-09 16:34 出处:网络
I have a polymorphic Location model: class Location < ActiveRecord::Base 开发者_运维知识库 acts_as_mappable

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 
0

精彩评论

暂无评论...
验证码 换一张
取 消