首先,按照惯例,型号名称应为EnrolledIn
,而不是Enrolled_in
。更好的是,将它改为Enrollment
或者作为名词具有明确含义的东西。而你也将遇到试图使用Class
(这已经是一个Ruby对象)作为模型名称的麻烦。也许将其更改为Course
或类似的东西。
这且不说,你应该在你迁移定义的所有列。您可以定义enrolled_ins
表是这样的:
add_table :enrolled_ins do |t|
t.references :student
t.references :course # I'm using course instead of class as noted above
end
的references
快捷方式添加:student_id数据和:COURSE_ID作为整数字段。
在你的模型文件,你必须:
# student.rb
class Student < ActiveRecord::Base
has_many :enrolled_ins
has_many :courses, :through => :enrolled_ins
end
# course.rb
class Course < ActiveRecord::Base
has_many :enrolled_ins
has_many :students, :through => :enrolled_ins
end
# enrolled_in.rb
class EnrolledIn < ActiveRecord::Base
belongs_to :student
belongs_to :course
end
谢谢你这么这么这么多! – user1436111