在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 :through =>; :location' 指令,我可以毫无问题地分配位置:
l = Location.create(:full_address=>'123 maple street, chicago, il')
u = User.create(:username=>'foo')
u.location = l
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
:location 的定义需要先于其使用:
The definition of :location need to precede its usage: