【发布时间】:2014-11-18 16:06:11
【问题描述】:
我有一个像这样的多态 PLACE 模型:
class Place < ActiveRecord::Base
belongs_to :placeable, polymorphic: :true
...
因此,例如,在我的 Lodging 模型中,我有类似的内容:
class Lodging < ActiveRecord::Base
has_one :place, as: :placeable, dependent: :destroy
...
它们按预期工作。 关键是现在我想创建一个 CarRental 模型,这个模型有两个地方,一个是接送地点,另一个是下车地点。
所以我写了:
class Transportation < ActiveRecord::Base
has_one :start_place, as: :placeable, dependent: :destroy
has_one :end_place, as: :placeable, dependent: :destroy
当然这行不通。关于如何做到这一点的任何见解?
编辑 1:如果我喜欢
class Transportation < ActiveRecord::Base
has_one :start_place, class_name: 'Place', as: :placeable, dependent: :destroy
has_one :end_place, class_name: 'Place', as: :placeable, dependent: :destroy
有效!但为什么?开始或结束信息保存在哪里?
** 编辑 2:不,它不起作用 ** 它不起作用... =(
【问题讨论】:
-
...它有效,但您确定它正确有效吗?据我所见,它应该返回相同的开始和结束。因为,正如你所说,没有地方可以存储这些信息。
-
你是对的。它不起作用@D-side。我的测试是错误的......
标签: ruby-on-rails activerecord polymorphic-associations has-one