Ruby on rails &引用;有两个;模型与多态模型的关系?

Ruby on rails &引用;有两个;模型与多态模型的关系?,ruby-on-rails,activerecord,polymorphic-associations,has-one,Ruby On Rails,Activerecord,Polymorphic Associations,Has One,我有一个多态场所模型,如下所示: class Place < ActiveRecord::Base belongs_to :placeable, polymorphic: :true ... classplace

我有一个多态场所模型,如下所示:

class Place < ActiveRecord::Base
  belongs_to :placeable, polymorphic: :true
...
classplace
因此,例如,在我的住宿模式中,我有如下内容:

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:不,它不工作**
它不起作用=(

你的模式对我来说没有意义-地方属于住宿等。当然有固定数量的地方,然后他们可以有很多不同的东西在其中?在你的模式中,同一个地方可能在数据库中很多次,这似乎是错的。我不认为多态关联也是解决这个问题的方法。

我会这样做:

class Place
  has_many :lodgings
  has_many :starting_transportations, :class_name => "Transportation", :as => :start_place
  has_many :ending_transportations, :class_name => "Transportation", :as => :end_place

class Lodging
  belongs_to :place #using lodgings.place_id

class Transportation
  belongs_to :start_place, :class_name => "Place" #via transportations.start_place_id
  belongs_to :end_place, :class_name => "Place" #via transportations.end_place_id
rails generate model StartPlace --no-migration --parent=Place
rails generate model EndPlace   --no-migration --parent=Place

顺便说一句,对于现实世界中的物理位置,我认为“位置”比“地点”更合适听起来太模糊了。

我想,以前的关联都是因为未知的类而抛出错误的。
有一个:start\u place
默认情况下,假设您指的是一个不存在的类
StartPlace
。它将术语单数化(尽可能地)并将其转换为
CamelCase
。一旦您指定了您的意思是
Place
,就很清楚了

无论如何,您都应该添加一个新列。让我们尝试单表继承(STI)。将类型为
string
的列
type
添加到
places
表:

rails generate migration AddTypeToPlaces type:string
…确保它做到了它所说的,然后迁移并创建新模型,如下所示:

class Place
  has_many :lodgings
  has_many :starting_transportations, :class_name => "Transportation", :as => :start_place
  has_many :ending_transportations, :class_name => "Transportation", :as => :end_place

class Lodging
  belongs_to :place #using lodgings.place_id

class Transportation
  belongs_to :start_place, :class_name => "Place" #via transportations.start_place_id
  belongs_to :end_place, :class_name => "Place" #via transportations.end_place_id
rails generate model StartPlace --no-migration --parent=Place
rails generate model EndPlace   --no-migration --parent=Place
注意:它们没有专门的表并从
Place
继承,而不是
ActiveRecord::Base
它应该生成两个空类,这很好,它们从
Place
继承东西

…然后将您的关联恢复到前一段时间不起作用的内容:

has_one :start_place, as: :placeable, dependent: :destroy
has_one :end_place,   as: :placeable, dependent: :destroy
…它们现在应该可以工作了,因为
StartPlace
被定义为

放置
,且
类型
等于
“StartPlace”

…与
EndPlace
相同,具有相应的
类型


我很久以前就描述过这一点。

谢谢@max williams的回答。你是对的,地名并不是最好的选择,正如你所说,我正在考虑将它改为地点。重点是我用从Google Places API收集的信息填充这个地方。因此,一次旅行有很多住宿和租车,而且它们都是有地方。所以你认为最好的解决方案是检查这个地方是否已经存在,如果不存在,只创建一个新的地方?这里的要点是,我允许用户编辑他们想要的任何地方信息(如街道、照片等).你能看一下我编辑的问题吗?我做了这些更改,效果很好……一般来说,你不应该在数据库中有两个相同的位置。想想这个问题。“既然我在X位置,这里有什么住处?”谢谢你的见解……我想这看起来很令人困惑,因为我一直在使用(并且会改变)这个“位置”的名称我的模型基本上是一个地址,lat lng等,所以每个日志都有一个位置(地址)……它有效,但你确定它正确吗?据我所见,它应该返回相同的开始和结束。因为,正如你所说,没有地方存储这些信息。你是对的。它在@D-side不起作用。我的测试是错误的……嘿@D-side,如果我有相同的开始和结束位置,我该怎么办?@Guido那么这个structure fails=)您可以使用
has\u many:through
through,方法是将相同的东西应用于连接模型(
startink
?),并保持
Place
不变。这取决于你的应用程序逻辑,什么是最好的。或者你可以有两个对象,并用一个方法比较它们是否在同一个地方。因为我将保持地点多态性,因为住宿、活动和其他东西可以有地点(地点),我想我还将在交通模型中添加一个地点。如果起始位置和结束位置相同,我将保存这一个位置。如果没有,我会保存它们,但我不会保存它,并创建一个类进行比较。你觉得怎么样?@Guido我对你的应用程序的逻辑还不太了解,无法给出建议。另一个解决方案是保留一个
位置
,并使用多个
归属
和对应的外键。