想象一下场景:
我有一堂课,有不同类型的学生。所有学生都有相似的属性,但每种类型的学生也有独特的属性。所以我使用 MTI 来保留表学生的共同属性和各自表中的个人属性,并在从班级角度处理学生类型时使用多态性来抽象学生类型。我遵循了本教程:http ://techspry.com/ruby_and_rails/multiple-table-inheritance-in-rails-3/ 。
从这里,我得到了这些模型:
class Clazz < ActiveRecord::Base
has_many :students
end
class Student < ActiveRecord::Base
belongs_to :stu, :polymorphic => true
belongs_to :clazz
end
class Student1 < ActiveRecord::Base
has_one :student, :as => :stu
end
class Student2 < ActiveRecord::Base
has_one :student, :as => :stu
end
当我想实例化一个特定的学生(通过学生与班级间接关联)时,我的问题就出现了。我不能在课堂上做到这一点,因为它与特定学生没有联系,当我尝试直接实例化它时,它说它无法识别 ':class' 字段。
Student1.new(:clazz => @clazz, ... [other atributes]...)
unknown attribute: :class
谁能给我一个关于如何做到这一点的提示?Tks