nat*_*vda 5 ruby ruby-on-rails polymorphic-associations
我正在使用遗留数据库,因此我无法控制数据模型.他们使用了很多多态链接/连接表,就像这样
create table person(per_ident, name, ...)
create table person_links(per_ident, obj_name, obj_r_ident)
create table report(rep_ident, name, ...)
Run Code Online (Sandbox Code Playgroud)
obj_nametable-name 在哪里,obj_r_ident是标识符.所以链接的报告将插入如下:
insert into person(1, ...)
insert into report(1, ...)
insert into report(2, ...)
insert into person_links(1, 'REPORT', 1)
insert into person_links(1, 'REPORT', 2)
Run Code Online (Sandbox Code Playgroud)
然后,人1将有2个链接报告,1和2.
我可以理解像这样的数据模型可能带来的好处,但我主要看到一个很大的缺点:使用约束不可能确保数据完整性.但是,唉,我不能再改变它了.
但是要在Rails中使用它,我正在寻找多态关联,但没有找到一个很好的方法来解决这个问题(因为我不能改变列名,并且没有找到办法做到这一点).
我确实提出了一个解决方案.请提供建议.
class Person < ActiveRecord::Base
set_primary_key "per_ident"
set_table_name "person"
has_and_belongs_to_many :reports,
:join_table => "person_links",
:foreign_key => "per_ident",
:association_foreign_key => "obj_r_ident",
:conditions => "OBJ_NAME='REPORT'"
end
class Report < ActiveRecord::Base
set_primary_key "rep_ident"
set_table_name "report"
has_and_belongs_to_many :persons,
:join_table => "person_links",
:foreign_key => "obj_r_ident",
:association_foreign_key => "per_ident",
:conditions => "OBJ_NAME='REPORT'"
end
Run Code Online (Sandbox Code Playgroud)
这有效,但我想知道是否会有更好的解决方案,使用多态关联.
小智 10
至少从Rails 4.2.1开始,您可以传递foreign_type给belongs_to声明以指定用于多态关联的"类型"的列的名称
http://apidock.com/rails/v4.2.1/ActiveRecord/Associations/ClassMethods/belongs_to
当然,您可以覆盖列名称,但是快速扫描 Rails API 并没有显示任何地方可以覆盖多态“类型”列。因此,您无法将其设置为“obj_name”。
它很难看,但我认为您的表中的每种类型的对象都需要一个 HABTM。
你也许可以做这样的事情:
{:report => 'REPORT'}.each do |sym, text|
has_and_belongs_to_many sym,
:join_table => "person_links",
:foreign_key => "obj_r_ident",
:association_foreign_key => "per_ident",
:conditions => "OBJ_NAME='#{text}'"
end
Run Code Online (Sandbox Code Playgroud)
至少这样所有常见的东西都保持干燥,并且您可以轻松添加更多关系。