Rails: Invalid single-table inheritance type error

北战南征 提交于 2019-12-02 22:54:51

I resolved this by setting the models inheritance_column to nil. Active Record Models can inherit from a table through the attribute :type, setting the inheritance_column to nil removes that attribute allowing you to have a database column named type

class Quote < ActiveRecord::Base
    self.inheritance_column = nil
end

I hate having potential gotchas deep in the code especially in the intial processes like generating a model. Better to just change the reserved word to something else and free yourself up to take advantage of inheritance column later if the need comes up. A cleaner solution is listed here -> rename a database column name using migration

It reads;

  1. Execute $> rails generate migration ChangeColumnName where, ChangeColumnName is the name of our migration. This can be any name.
  2. Now, edit the generated migration file at db/migrate/_change_column_name.rb

    class ChangeColumnName < ActiveRecord::Migration
    def change
    rename_column :table_name, :old_column, :new_column
    end
    end
    
  3. $> rake db:migrate

You will have to edit controller and view files e.g. if the model name is Product then you will likely edit these files

  1. /app/views/products/_form.html.erb
  2. /app/views/products/show.html.erb
  3. /app/controllers/products_controller.erb
  4. /app/views/products/index.html.erb
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!