回报率:HAS_ONE"或者其他与QUOT;? (或者,多态性没有继承。) [英] RoR: has_one "or the other"? (Or, polymorphism without the inheritance.)

查看:166
本文介绍了回报率:HAS_ONE"或者其他与QUOT;? (或者,多态性没有继承。)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我有话一个有趣的要求我的项目。我需要一个 HAS_ONE 关系,它可以是一个类或其他,但没有继承。我可以摆脱继承,如果它是唯一的出路,但这两个副记录具有完全不同的数据,在所有都没有关系。

Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all.

我需要搞清楚的是像下面这样。

What I need to figure out is something like the following.

# 1. Foo never belongs to anything.
# 2. Foo MUST have one assigned sub-record for validity.
# 3. Foo can only have either Bar or Baz assigned.
# 4. Bar and Baz have only ONE common property, and aren't
#    related in either data or implementation.

class Foo < ActiveRecord::Base
  # Attributes: id, name, value
  has_one :assignment, :foreign_key => 'assigned_to', :readonly => true
          # Could really use an :object_type for has_one here...
end

class Bar < ActiveRecord::Base
  # Attributes: name,...
end

class Baz < ActiveRecord::Base
  # Attributes: name,...
end

其中,有一个分配类型,无论是酒吧巴兹;他们只有一个共同的列,所以也许我可以从一个父对象。但是,如果我让他们从一个共同的目标继承(当它们包含真正的数据,桔子和苹果),我必须做一个表中的记录?我也许可以逃脱它如果记录是一个抽象的记录,但这些孩子是不是?

Where Foo has one assignment, of type either Bar or Baz; they only share one common column, so perhaps I can make a parent object from that. However, if I make them inherit from a common object (when the data they contain really is oranges and apples) must I make a table for the record? Can I perhaps get away with it if the record is an abstract record, but the children aren't?

我想,现在你可以看到我的难处。我是相当新的回报率,但爱它为止。我敢肯定有解决的办法,但我会该死的,如果我无法弄清楚它是什么。

I suppose by now you can see my difficulty. I'm rather new to RoR but loving it so far. I'm sure there's a way around this, but I'll be darned if I can't figure out what it is.

推荐答案

您正在尝试一些不适合关系数据库范式建模。在SQL中所有的引用有一个原产地和一个目标。

You're trying to model something that doesn't fit the relational database paradigm. All references in SQL have one origin and one target.

FWIW,多态关联也是一个反模式,因为它打破了这个规则。这应该是一个线索,这是一个破碎的设计时的文件的说,你必须放弃一个参照完整性约束,使其工作!

FWIW, Polymorphic Associations is also an anti-pattern because it breaks this rule. It should be a clue that it's a broken design when the documentation says you must forgo a referential integrity constraint to make it work!

您需要美孚有两个 HAS_ONE 关系:一个酒吧,一个巴兹。然后执行一些一流的逻辑来尽量保证只有一个参考的填充美孚的任何实例。也就是说,引用了酒吧和巴兹,必须有一个值,另一个必须是零,但这是你的code,检查和执行。

You need Foo to have two has_one relationships: one to Bar and one to Baz. Then implement some class logic to try to ensure only one reference is populated in any instance of Foo. That is, of the references to Bar and Baz, one must have a value and the other must be nil, but this is something for your code to check for and enforce.

这篇关于回报率:HAS_ONE&QUOT;或者其他与QUOT;? (或者,多态性没有继承。)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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