繁体   English   中英

在多态关联中更改类型标识符

[英]Change type identifier in polymorphic association

我试图以一种有点奇怪的方式使用Rails的多态关联,我遇到了一个问题。

多态表是Address

class Address < ActiveRecord::Base
  belongs_to :addressable, polymorphic: true
end

我的数据库有唯一性约束,因此无法添加相同的地址关联两次。

我也有一个需要两个地址的Trip模型。 一个是旅行的起源,另一个是旅行目的地。

class Trip < ActiveRecord::Base
  has_one :origin, as: :addressable, class_name: 'Address'
  has_one :destination, as: :addressable, class_name: 'Address'
end

问题是当Rails创建一个与trip相关联的地址时,它使用类名(即“Trip”)来填充addressable_type列。 这意味着如果我尝试使用origin和destination进行旅行,rails会尝试使用相同的addressable_typeaddressable_id添加两行。 这显然在唯一性约束下失败了。

我可以删除唯一性约束,但之后我会最终得到重复的记录,这会混淆Rails,因为它不知道哪个记录是起源,哪个是目的地。

我真正想要做的是指定用于addressable_type的字符串:

class Trip < ActiveRecord::Base
  has_one :origin, as: :addressable, class_name: 'Address', type: 'Trip Origin'
  has_one :destination, as: :addressable, class_name: 'Address', type: 'Trip Destination'
end

那可能吗? 是否有其他解决方案或我是否需要重新考虑我的数据库架构?

我原以为address不应该belongs_to旅行,因为一个地址可能是多次旅行的起源和/或目的地。 如果您有唯一性约束,则尤其如此。 外键应存储在行程中:

class Address < ActiveRecord::Base
  has_many :trips_as_origin, class_name: "Trip", foreign_key: "origin_id"
  has_many :trips_as_destination, class_name: "Trip", foreign_key: "destination_id"
end

class Trip < ActiveRecord::Base
  belongs_to :origin, class_name: "Address"
  belongs_to :destination, class_name "Address"
end

您需要创建一个将origin_iddestination_id添加到Trip的迁移。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM