Kva*_*ass 77 ruby-on-rails single-table-inheritance ruby-on-rails-3
我想使用一个名为type
不调用单表继承(STI)的列 - 我只想type
成为一个普通的列来保存一个String
.
如果没有Rails期望我有单表继承并抛出异常,我怎么能这样做The single-table inheritance mechanism failed to locate the subclass...This error is raised because the column 'type' is reserved for storing the class in case of inheritance.
呢?
关于如何做到这一点的任何想法?
Val*_*cev 127
在Rails 3.1 set_inheritance_column
中不推荐使用,你也可以使用它nil
作为名称,如下所示:
class Pancakes < ActiveRecord::Base
self.inheritance_column = nil
#...
end
Run Code Online (Sandbox Code Playgroud)
mu *_*ort 20
您可以使用set_inheritance_column
以下方式覆盖STI列名称:
class Pancakes < ActiveRecord::Base
set_inheritance_column 'something_you_will_not_use'
#...
end
Run Code Online (Sandbox Code Playgroud)
因此,选择一些您不会用于任何内容的列名称并将其提供给set_inheritance_column
.
rii*_*rii 20
我知道这个问题相当陈旧,这与您提出的问题略有不同,但每当我觉得命名列类型或something_type的冲动时,我总是这样做,我会搜索类型的同义词并使用它:
以下是几种选择:种类,种类,品种,类别,集合,类型,种类,订单等.
Rails 4.x
我在Rails 4
应用程序中遇到了问题,但在Rails 4中,该set_inheritance_column
方法根本不存在,因此您无法使用它.
为我工作的解决方案是通过覆盖禁用单表继承ActiveRecord
的inheritance_column
方法,就像这样:
class MyModel < ActiveRecord::Base
private
def self.inheritance_column
nil
end
end
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!
归档时间: |
|
查看次数: |
25678 次 |
最近记录: |