Rails多态关联(旧版数据库) [英] rails polymorphic association (legacy database)

查看:95
本文介绍了Rails多态关联(旧版数据库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用旧版数据库,因此我对数据模型没有任何控制权.他们使用了很多多态的link/join-tables,就像这样

I am using a legacy database, so i do not have any control over the datamodel. They use a lot of polymorphic link/join-tables, like this

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是标识符. 因此,链接的报告将如下插入:

where obj_name is the table-name, and obj_r_ident is the identifier. So linked reports would be inserted as follows:

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.

And then person 1 would have 2 linked reports, 1 and 2.

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

I can understand possible benefits having a datamodel like this, but i mostly see one big shortcoming: using constraints is not possible to ensure data integrity. But alas, i cannot change this anymore.

但是要在Rails中使用它,我一直在研究多态关联,但是没有找到解决此问题的好方法(因为我无法更改column-names,也没有找到一种方法来做到这一点).

But to use this in Rails, i was looking at polymorphic associations but did not find a nice way to solve this (since i cannot change the columns-names, and did not readily find a way to do that).

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

I did come up with a solution though. Please provide suggestions.

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

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

This works, but i wonder if there would be a better solution, using polymorphic associations.

推荐答案

您可以覆盖列名,但是对Rails API的快速扫描并未显示出要覆盖多态'type'列的任何地方.因此,您将无法将其设置为"obj_name".

You can override the column names, sure, but a quick scan of the Rails API didn't show me anywhere to override the polymorphic 'type' column. So, you wouldn't be able to set that to 'obj_name'.

这很丑陋,但是我认为您需要为表中的每种对象类型使用HABTM.

It's ugly, but I think you'll need a HABTM for each type of object in your table.

可能可以执行以下操作:

{: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 ,您可以轻松添加更多的关系.

At least that way all the common stuff stays DRY and you can easily add more relationships.

这篇关于Rails多态关联(旧版数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆