简体   繁体   English

在单独的表中与 id 和类型的多态关联

[英]Polymorphic association with id and type in separate tables

I have a polymorphic relation in my application for custom fields similar to this:我的应用程序中有一个类似于此的自定义字段的多态关系:

class CustomFieldValue < ApplicationRecord
  belongs_to :custom_field_type
  belongs_to :custom_field_valuable, polymorphic: true
end
class CustomFieldType < ApplicationRecord
  has_many :custom_field_values
end

Basically the custom_field_type table holds a few details about the field type (selection, checkbox etc) as well as a 'model_type' column for the model the custom field belongs to.基本上,custom_field_type 表包含有关字段类型(选择、复选框等)的一些详细信息,以及自定义字段所属模型的“model_type”列。

The custom_field_value table has the expected polymorphic columns 'custom_field_valuable_type' and 'custom_field_valuable_id' which works fine but seeing as custom_field_value belongs to custom_field_type which already has the model_type stored, I was wondering if there was a way I can do away with the custom_field_valuable_type? custom_field_value 表具有预期的多态列“custom_field_valueable_type”和“custom_field_valuable_id”,它们工作正常,但看到 custom_field_value 属于已经存储了 model_type 的 custom_field_type,我想知道是否有办法取消 custom_field_valueable_type?

I've tried redefining 'custom_field_valuable_type' with我试过重新定义“custom_field_valuable_type”

def custom_field_valuabkle_type
  self.custom_field_type.model_type
end

But that doesn't work as there is no column to query.但这不起作用,因为没有要查询的列。

I was wondering if there was a way I can do away with the custom_field_valuable_type?我想知道是否有办法取消 custom_field_valueable_type?

No. Polymorphic associations expect a association_name_type column to exist on the table with the association.多态关联期待association_name_type栏上的关联表中。 Putting the type column another table is not possible AFAIK and would be really over-complicated. AFAIK 不可能将类型列放在另一个表中,而且会非常复杂。 Polymorphic associations are already a hacky solution to the Object-relational impedance mismatch problem and you're going from saying that the foreign key is constructed from two columns to the foreign key is constructed of from columns on different tables!多态关联已经是对象-关系阻抗不匹配问题的一个hacky 解决方案,您从说外键是由两列构造到外键是由不同表上的列构造的! That doesn't improve anything and just increases the mismatch.这不会改善任何事情,只会增加不匹配。 Your approach is not going to work as the join queries are done in the database and don't call your getter method.您的方法将不起作用,因为连接查询是在数据库中完成的,并且不会调用您的 getter 方法。

I think you're also confusing two completely different concepts.我认为您也混淆了两个完全不同的概念。 What you are doing is just a variation on the Entity Attribute Value (EAV) pattern.您正在做的只是实体属性值 (EAV) 模式的变体。 What your CustomFieldType provides to the setup is normalization of the attributes - so that your not duplicating the definition of attributes with each value.您的 CustomFieldType 为设置提供的是属性的规范化 - 这样您就不会用每个值复制属性的定义。 This has nothing to do with linking Value -> Entity.这与链接 Value -> Entity 无关。

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

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