Rails 错误未初始化常量Assignment::AssignmentsCourse 中的Has Many Through 关联
我收到错误“未初始化常量Assignment::AssignmentsCourse”。这是我的模型:
assignment.rb
class Assignment < ActiveRecord::Base
has_many :assignmentsCourses
has_many :courses, :through => :assignmentsCourses
attr_accessible :name, :dateAssigned, :dateDue, :description, :weight, :category_tokens
attr_reader :category_tokens
def category_tokens=(ids)
puts 'el ids: ', ids.split(",")
self.courseIds = ids.split(",")
end
end
course.rb
class Course < ActiveRecord::Base
has_and_belongs_to_many :assignments
end
AssignmentCourse.rb
class AssignmentCourse < ActiveRecord::Base
belongs_to :assignment
belongs_to :course
attr_accessible :assignment_id, :course_id
end
I'm getting the error "uninitialized constant Assignment::AssignmentsCourse". Here are my models:
assignment.rb
class Assignment < ActiveRecord::Base
has_many :assignmentsCourses
has_many :courses, :through => :assignmentsCourses
attr_accessible :name, :dateAssigned, :dateDue, :description, :weight, :category_tokens
attr_reader :category_tokens
def category_tokens=(ids)
puts 'el ids: ', ids.split(",")
self.courseIds = ids.split(",")
end
end
course.rb
class Course < ActiveRecord::Base
has_and_belongs_to_many :assignments
end
AssignmentCourse.rb
class AssignmentCourse < ActiveRecord::Base
belongs_to :assignment
belongs_to :course
attr_accessible :assignment_id, :course_id
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个字段和所有字段都不应该是驼峰式大小写,它不是 ruby 风格,并且会破坏类加载。结尾也只能是复数形式,而不是两个词。在幕后,activerecord 会取消您提供的符号的复数,并执行类似于
require
的类加载。例如,如果您尝试require 'activeRecord'
则不起作用。 Ruby 使用下划线来派生多单词类名称。应该是:
has_many :assignment_courses
也可以更改 has much 。你的访问器不应该是驼峰式的,或者 ruby_style_is_to_underscore 。
This and all of your fields should not be camel cased it is not ruby style and it breaks the class loading. The end should only be pluralized too, not both words. Behind the scenes activerecord depluralizes the symbol you provide and does class loading similar to
require
. If you triedrequire 'activeRecord'
that would not work for example. Ruby uses underscores to derive multi word class names.It should be:
has_many :assignment_courses
Change the has many though too. Your accessors should not be camel cased either ruby_style_is_to_underscore.