控制器中的Rails 4.0.0错误:ActiveRecord :: SubclassNotFound

时间:2015-04-28 22:48:42

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord

您好我的控制器中出现此错误:

ActiveRecord::SubclassNotFound in CoursesController#create_course
Invalid single-table inheritance type: CourseTemplate is not a subclass of Course

此行发生错误:

course = Course.new(self.attributes)

我该怎么做才能解决这个问题?

我已经尝试过在模型中做到这一点:

self.inheritance_column = :_type_disabled

但后来我收到了这个错误:

Mysql2::Error: Duplicate entry '4' for key 'PRIMARY': INSERT INTO `course_objects`

出现此错误,因为再次尝试使用course_object的ID,id是PK

1 个答案:

答案 0 :(得分:1)

您必须从旧对象属性中删除id:

obj_attributes = self.attributes
obj_attributes.delete("id") 
course = Course.new(obj_attributes)