Rails Error uninitialized constant Assignment::AssignmentsCourse in Has Many Through Association
-
03-12-2019 - |
Question
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
Solution
has_many :assignmentsCourses
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 tried require '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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow