在Rails控制台中分配多态acts_as_mappable模型

发布于 2024-10-17 06:34:57 字数 906 浏览 1 评论 0原文

我有一个多态位置模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟织青萝梦 2024-10-24 06:34:57

:location 的定义需要先于其使用:

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

The definition of :location need to precede its usage:

class User < ActiveRecord::Base
  has_one :location, :as => :locatable
  acts_as_mappable :through => :location
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文