Ruby-on-rails Rails——使用没有 STI 的类型列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7134559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:49:30  来源:igfitidea点击:

Rails -- use type column without STI?

ruby-on-railsruby-on-rails-3single-table-inheritance

提问by Kvass

I want to use a column called typewithout invoking Single Table Inheritance (STI) - I just want typeto be a normal column that holds a String.

我想在type不调用单表继承 (STI) 的情况下使用一个名为的列- 我只想type成为一个包含String.

How can I do this without having Rails expecting me to have single table inheritance and throwing an exception of 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.?

如何在不让 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.

Any ideas on how to do this?

关于如何做到这一点的任何想法?

回答by Valentin Nemcev

In Rails 3.1 set_inheritance_columnis deprecated, also you can just use nilas a name, like this:

在 Rails 3.1set_inheritance_column中已弃用,您也可以将其nil用作名称,如下所示:

class Pancakes < ActiveRecord::Base
    self.inheritance_column = nil
    #...
end

回答by rii

I know this question is rather old and this deviates a bit from the question you are asking, but what I always do whenever I feel the urge to name a column type or something_type is I search for a synonym of type and use that instead:

我知道这个问题很旧,这与您提出的问题略有不同,但是每当我感到想要命名列类型或 something_type 时,我总是会搜索类型的同义词并使用它来代替:

Here are a couple alternatives: kind, sort, variety, category, set, genre, species, order etc.

这里有几个替代方案:种类、排序、品种、类别、集合、流派、物种、顺序等。

回答by mu is too short

You can override the STI column name using set_inheritance_column:

您可以使用set_inheritance_column以下方法覆盖 STI 列名称:

class Pancakes < ActiveRecord::Base
    set_inheritance_column 'something_you_will_not_use'
    #...
end

So pick some column name that you won't use for anything and feed that to set_inheritance_column.

因此,请选择一些您不会将其用于任何内容的列名称并将其提供给set_inheritance_column.

回答by Itay Grudev

Rails 4.x

导轨 4.x

I encountered the problem in a Rails 4app, but in Rails 4the set_inheritance_columnmethod does not exist at all so you can't use it.

我在一个Rails 4应用程序中遇到了这个问题,但在Rails 4 中,set_inheritance_column方法根本不存在,因此您无法使用它。

The solution that worked for me was to disable the single table inheritance by overriding ActiveRecord's inheritance_columnmethod, like this:

对我有用的解决方案是通过覆盖ActiveRecordinheritance_column方法禁用单表继承,如下所示:

class MyModel < ActiveRecord::Base

  private

  def self.inheritance_column
    nil
  end

end

Hope it helps!

希望能帮助到你!

回答by Bill Lipa

If you want to do this for all models, you can stick this in an initializer.

如果你想对所有模型都这样做,你可以把它放在初始化程序中。

ActiveSupport.on_load(:active_record) do
  class ::ActiveRecord::Base
    # disable STI to allow columns named "type"
    self.inheritance_column = :_type_disabled
  end
end