单表继承引用具有自己的字段的子类 [英] Single Table Inheritance to refer to a child class with its own fields

查看:163
本文介绍了单表继承引用具有自己的字段的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Ruby on Rails 3,我实现了一个工作单表继承,如下所示:

I am using Ruby on Rails 3 and I implemented a working Single Table Inheritance like the following:

class User < ActiveRecord::Base

  # Schema Information
  #
  # Table name: User
  #
  # id              : integer
  # type            : string
  # children_user_id: integer

  ...
end

class UserAdmin < User

  # Schema Information
  #
  # Table name: UserAdmin
  #
  # id             : integer
  # special_field1 : string
  # special_field2 : string
  # ...

  ...
end

class UserCommon < User

  # Schema Information
  #
  # Table name: UserCommon
  #
  # id             : integer
  # another_field1 : string
  # another_field2 : string
  # ...

  ...
end

我想知道在创建 UserAdmin 记录(或 UserCommon 记录)在用户表中运行以下

I would like to know if, on creating an UserAdmin record (or an UserCommon record) in the User table running the following

UserAdmin.create(:children_user_id => "1")
# or UserCommon.create(:children_user_id => "1")

有可能在 UserAdmin 中自动创建一个新记录(可能是 Rails Way $ c>表(或 UserCommon 表),它有自己的字段(在数据库级别,这些字段是列)。为了更好地处理 UserAdmin ,我想这样做,因为这个类有不同的和更多的属性 UserCommon class。

it is possible to "automatically" create in someway (possibly the "Rails Way"!) a new record also in the UserAdmin table (or UserCommon table) that has its own fields (at the database level, those fields are columns). I would like to do that in order to "better handle" UserAdmin because this class has different and more attributes of the UserCommon class.

如果可以,我该怎么做(可能使用关联模型语句,回调,多态性...)

If it is possible, how can I do that (maybe using association model statements, callbacks, polymorphism, ...)? Do you have some advice on this issue?

推荐答案

单表继承的事情是基于单个表模型,不足为奇,因此你不能使用不同的表使用不同的表。

The thing with single table inheritance is it is based on a single table model, not surprisingly, so you can't have different classes using different tables.

通常,Rails方法是将所有可能的字段捆绑到单个表中,并使用STI来处理数据映射和验证问题。然而,它可以隐藏的应用程序的限制,虽然,因为通常一个字段被定义意味着它可以被绑定到该表的任何类使用。大多数人不认为这是一个问题。

Typically the "Rails Way" is to bundle together all the possible fields that are required into a single table and use STI to handle the data mapping and validation issues for you. There are limits on what it can hide from the application, though, as generally a field being defined means it's usable by any of the classes bound to that table. Most people do not consider this to be an issue.

您可能想要做的是根据用户类型创建加入的记录,例如:

What you probably want to do is make a record that's joined in depending on the user type, for instance:

class User < ActiveRecord::Base
end

class AdminUser < User
  belongs_to :admin_profile
end

class CommonUser < User
  belongs_to :common_profile
end

这需要<$ c $具有 admin_profile_id common_profile_id 列的表 c > admin_profiles common_profiles 表包含所需的其他字段。

This would require the users table having an admin_profile_id and common_profile_id column where the admin_profiles and common_profiles tables contain the required additional fields.

可以使用委托方法将表映射回基类。

The attributes in these tables can be mapped back into the base class using the delegate method as required.

将额外字段移动到单独的表可能有助于区分事物,但也意味着由于所需的连接和由于一个部分缺失或过期而导致的不一致记录的可能性增加,读取将会更慢。

Moving the extra fields into a separate table may help compartmentalize things, but it also means reads will be slower because of the required join and the possibility of inconsistent records, due to one part being missing or out of date, is increased.

通常,你可以将所有的用户相关字段加载到单个表中,即使这些字段中的很多字段不经常使用。 NULL字段的存储成本在事物方案中通常较低,并且除非存在数百个这样的字段,否则暴露给开发者的额外复杂性是最小的,比起必须成对地创建和引用记录的更小的支付价格。

Generally you are okay loading all of the user-related fields into a single table even if many of these fields are not frequently used. The storage cost of a NULL field is typically low in the scheme of things and unless there are hundreds of these fields, the additional complexity exposed to the developer is minimal, a smaller price to pay than to have to create and reference records in pairs constantly.

这篇关于单表继承引用具有自己的字段的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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