Change ActiveRecord::Base.inheritance_column in a rails app

六月ゝ 毕业季﹏ 提交于 2019-12-07 03:35:57

问题


I want to use Single Table Inheritance using a column other than type. According to the Rails documentation - http://api.rubyonrails.org/classes/ActiveRecord/Base.html, I can do this by modifying ActiveRecord::Base.inheritance_column. How can I do this?


回答1:


Try the following:

class MyModel < ActiveRecord::Base
    self.inheritance_column = 'column_that_is_not_type'
end

Your migrations should work everywhere.




回答2:


@Vidya's answer is correct but a better way is:

class MyModel < ActiveRecord::Base
  # disable STI
  self.inheritance_column = :_type_disabled
end


来源:https://stackoverflow.com/questions/20294879/change-activerecordbase-inheritance-column-in-a-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!