Rails错误ActiveRecord :: SubclassNotFound

时间:2015-08-10 10:34:21

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

我收到了这个错误:

enter image description here

代码如下所示:

def save_as_template
  obj_attributes = self.attributes
  obj_attributes.delete("id")

  course = CourseTemplate.new(obj_attributes)
  course.start_date = nil
  course.active = false
  course.save

但我不能这样做:

 self.inheritance_column = :_type_disabled

因为其他事情出了问题......我该怎么办?

class CourseTemplate < CourseObject
  #self.inheritance_column = :_type_disabled
  validates_presence_of :name
  validates_presence_of :description
  validates_numericality_of :instant_questions, :check_questions,    :repeat_questions, :mindmail_interval, :mindmail_reminder_interval
  before_validation :before_validation_set_default_values


class CourseObject < ActiveRecord::Base
  belongs_to :lecturer, :class_name => "User"
  has_many :lessons, ->{order "lessons.sort ASC"}, :dependent => :destroy

  validate :language_allowed

class Course < CourseObject

  self.inheritance_column = :_type_disabled
  has_and_belongs_to_many :clients, :join_table => :clients_courses # TODO c
  has_many :memberships, :dependent => :destroy
  has_many :users, :through => :memberships
  has_and_belongs_to_many :user_groups

1 个答案:

答案 0 :(得分:1)

我想只是一个猜测:obj_attributes哈希仍然包含inheritance_column键的值 - 不能在传递给CourseTemplate.new的哈希值中。 但是你应该dup哈希而不是从属性哈希中删除它 - 我不确定但是直接修改这个哈希可能会影响你调用save_as_template的对象。

不要篡改inheritance_column。当您为这些对象使用构造函数时,这由active_record正确管理。

  obj_attributes = self.attributes.dup
  obj_attributes.delete("id")
  obj_attributes.delete("inheritance_column")

  course = CourseTemplate.new(obj_attributes)