rails多态关联(遗留数据库)

我正在使用遗留数据库,因此我无法控制数据模型。 他们使用了很多多态链接/连接表,就像这样

create table person(per_ident, name, ...) create table person_links(per_ident, obj_name, obj_r_ident) create table report(rep_ident, name, ...) 

其中obj_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) 

然后,人1将有2个链接的报告,1和2。

我可以理解像这样的数据模型可能带来的好处,但我主要看到一个很大的缺点:使用约束不可能确保数据完整性。 但是,我不能再改变它了。

但是要在Rails中使用它,我正在寻找多态关联,但没有找到一个很好的方法来解决这个问题(因为我无法更改列名,并且没有找到办法做到这一点)。

我确实提出了一个解决方案。 请提供建议。

 class Person  "person_links", :foreign_key => "per_ident", :association_foreign_key => "obj_r_ident", :conditions => "OBJ_NAME='REPORT'" end class Report  "person_links", :foreign_key => "obj_r_ident", :association_foreign_key => "per_ident", :conditions => "OBJ_NAME='REPORT'" end 

这有效,但我想知道是否会有更好的解决方案,使用多态关联。

当然,您可以覆盖列名称,但是快速扫描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 

至少在这种情况下,所有常见的东西都会保持DRY ,您可以轻松添加更多关系。

至少从Rails 4.2.1开始,您可以将foreign_type传递给belongs_to声明,以指定用于多态关联的“类型”的列的名称

http://apidock.com/rails/v4.2.1/ActiveRecord/Associations/ClassMethods/belongs_to