rails不能在model中使用type作为字段名的解决办法

Ruby | 2018-04-01 21:40:53 | 阅读 1420 次 | 评论(0)

使用type作为字段名会出现这个错误:

ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'xxxx'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite SmsRecord.inheritance_column to use another column for that information.


解决办法:

在相关model中加入“self.inheritance_column = nil”即可,如:

class SmsRecord < ApplicationRecord
	self.inheritance_column = nil
end


-------------------------------- 作者在 2018-04-01 21:46:38 补充以下内容 --------------------------------

相关参考:

https://stackoverflow.com/questions/11984893/issue-with-column-name-type-in-rails-3

https://stackoverflow.com/questions/7134559/rails-use-type-column-without-sti

https://apidock.com/rails/ActiveRecord/ModelSchema/ClassMethods/inheritance_column

文章评论,共0条
游客请输入验证码
浏览1923536次
文章归档
最新评论
  • Miss_Zou:不都是复制粘贴的吗
  • Miss_Zou:目测是大佬
  • 神犇dengyuhy:语文很好