Rails 单表继承
对于单表继承,如何强制 Rails 使用整数列而不是字符串作为“类型”列?
For single table inheritance, how do you force Rails to use an integer column for the 'type' column instead of string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以重写 Rails 用于将表名转换为类名的方法,反之亦然:
相关方法是
find_sti_class
,它负责将类型列中存储的值转换为相应的 ActiveRecord 模型,并且sti_name
负责检索存储在给定 ActiveRecord 子类的类型列中的值。您可以像这样覆盖它们:
我写了一篇 post 详细阐述更详细地说。
You can override the methods Rails uses to convert the table name to class name and vice versa:
The relevant methods are
find_sti_class
which is responsible for the translating the value stored in the type column to the respective ActiveRecord model andsti_name
which is responsible for retriving the value stored in type column given an ActiveRecord subclass.You can override them like this:
I have written a post elaborating this in more detail.
您必须找到 ActiveRecord 中负责处理“类型”列的部分并对其进行猴子修补,即覆盖它在应用程序中的工作方式。
You would have to find the part of ActiveRecord responsible for handling the "type" column and monkey patch it, i.e. override how it worked from within your application.