Rails 3 中的类表继承
我目前正在开发一个 Rails 3 应用程序,它看起来可能需要对几个模型使用类表继承。
这是正在发生的事情的一个简化示例。
我有一个名为 Person 的类,具有诸如姓名、电子邮件、密码之类的通用属性,这些属性对于应用程序中的所有类型的人来说都是通用的,并用于身份验证。
人有两个子类(或两种类型的人......):驾驶员和乘客。这两个子类都共享 Person 的通用属性,但又具有它们自己独有的特定附加属性。 (例如,驾驶员可以拥有许多车辆和许可证,但乘客则不会)
对于这种情况,我将如何实施 CTI?我一直在查看此处提供的示例:
http: //rhnh.net/2010/08/15/class-table-inheritance-and-eager-loading
但它并没有推测如何从 Driver 或 Passenger 对象访问 Person 的公共属性,并且我对此有点困惑。
特别是,我想知道的是:
如果我要更新 Driver 的属性,如何轻松访问和更新父 People 表上的相关属性?我是否必须挂钩 after_save 回调并分离出哪个属性更新去哪里?或者有更好的方法来解决这个问题吗?
I'm currently working on a Rails 3 application that looks like it might need to use Class Table Inheritance for a couple of models.
A simplified example of what's going on is this.
I have a class called Person with general attributes like name, email, password which are common to all types of people in the application and used for authentication.
There are two subclasses to Person (or two types of people...), Driver and Passenger. Both of these subclasses share the generic attributes of Person but then have specific additional attributes which are unique to themselves. (for example a Driver can have many Vehicles and Licenses but a Passenger would not)
How would I implement CTI for this kind of situation? I've been looking at an example provided here:
http://rhnh.net/2010/08/15/class-table-inheritance-and-eager-loading
But it doesn't speculate on how to access the common attributes of a Person from a Driver or Passenger object and I'm a bit confused by that.
In particular, what I'd like to know is:
If I'm updating the attributes of a Driver, how can I easily access and update the relevant attributes on the parent people table? Do I have to hook into an after_save callback and separate out which attribute update goes where? Or is there a better way to approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
还有一个插件“acts_as_relation”可以做到这一点,
https://github.com/hzamani/acts_as_relation/
在您的情况下,代码将是这样的:
Don不要忘记将
person_type
和person_id
列添加到persons
表中。现在 Drive 和 Passenger 都继承了 Person 属性、验证和方法。
Also there is a plugin 'acts_as_relation' to do this,
https://github.com/hzamani/acts_as_relation/
in your case the code will be this:
Don't forget to add
person_type
andperson_id
columns topersons
table.Now both Drive and Passenger inherit Person attributes, validations and methods.
为什么不使用单表继承?例如:
通过这种方式,您将拥有一个公共类 Person,以及从它派生的两个特定类
why not using Single Table Inheritance? for example:
in this way you'll have a common class Person, plus two specific classes derived from it
我正在使用类表继承插件,它运行良好, http://github.com/brunofrank /类表继承
I'm using the class table inheritance plugin and it working well, http://github.com/brunofrank/class-table-inheritance
我最近分叉了一个很有前景的项目,在 Rails 中实现多表继承和类继承。我花了几天时间对其进行快速开发、修复、评论和文档,并将其重新发布为 CITIER(Rails 的类继承和表继承嵌入)。
我认为您可以将其与上面的答案结合起来?
考虑看看: http://peterhamilton.github.com/citier
我查看了类似的解决方案类表继承存储库,但这在我看来更干净、更符合逻辑。
它还支持主要的 SQL 语言 SQLite、MySQL、PostgreSQL 以及可能更多,但我还没有测试过它们。
I recently forked a promising project to implement multiple table inheritance and class inheritance in Rails. I have spent a few days subjecting it to rapid development, fixes, commenting and documentation and have re-released it as CITIER (Class Inheritance and Table Inheritance Embeddings for Rails).
I think you could potentially combine it with the answers above?
Consider giving it a look: http://peterhamilton.github.com/citier
I looked at solutions like the class-table-inheritance repo, but this is much cleaner and more logical IMO.
It also supports the main SQL languages SQLite, MySQL, PostgreSQL and probably more but I haven;t tested them.