Ruby on rails 使用关系的未初始化常量错误

Ruby on rails 使用关系的未初始化常量错误,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我按照说明创建了一个模型课,其中有一个学生和一个老师(都是模型用户)以及一个课程开始日期 #Lesson Controller class Lesson < ActiveRecord::Base belongs_to :student, class_name => 'User' belongs_to :teacher, class_name => 'User' end #User Controller class User < ActiveRecord::Bas

我按照说明创建了一个模型课,其中有一个学生和一个老师(都是模型用户)以及一个课程开始日期

#Lesson Controller
class Lesson < ActiveRecord::Base
  belongs_to :student, class_name => 'User'
  belongs_to :teacher, class_name => 'User'
end

#User Controller
class User < ActiveRecord::Base
  has_many :lessons_to_attend, :class_name => 'Lesson', :foreign_key => 'student_id'
  has_many :lessons_to_teach, :class_name => 'Lesson', :foreign_key => 'teacher_id'
end
两件事:

belongs_to :student, class_name => 'User'
belongs_to :teacher, class_name => 'User'
类名上的语法错误。这应该是
:class\u name=>“User”
class\u name:“User”

另一件事是,我认为您需要在关联的两侧设置您的
反向

class Lesson < ActiveRecord::Base
  belongs_to :student, class_name: 'User', inverse_of: :lessons_to_attend
  belongs_to :teacher, class_name: 'User', inverse_of: :lessons_to_teach
end

class User < ActiveRecord::Base
  has_many :lessons_to_attend, class_name: 'Lesson', foreign_key: 'student_id', inverse_of: :student
  has_many :lessons_to_teach, class_name: 'Lesson', foreign_key: 'teacher_id', inverse_of: :teacher
end
课程
这里有一个输入错误:
属于:学生,班级名称=>“用户”
,应该是
:班级名称
。啊,太简单了!谢谢你
belongs_to :student, class_name => 'User'
belongs_to :teacher, class_name => 'User'
class Lesson < ActiveRecord::Base
  belongs_to :student, class_name: 'User', inverse_of: :lessons_to_attend
  belongs_to :teacher, class_name: 'User', inverse_of: :lessons_to_teach
end

class User < ActiveRecord::Base
  has_many :lessons_to_attend, class_name: 'Lesson', foreign_key: 'student_id', inverse_of: :student
  has_many :lessons_to_teach, class_name: 'Lesson', foreign_key: 'teacher_id', inverse_of: :teacher
end