Basically I'm using many-to-many
relationship with through
to combine Skill
and Course
models. So it looks like this:
Course
has_many :course_skills
has_many :skills, through: :course_skills
Skill
has_many :course_skills
has_many :courses, through: :course_skills
CourseSkills
belongs_to :skill
belongs_to :course
And I'd like to be able to add many skills to courses so the view looks like this:
<div class="field">
<%= f.label :skill, "Skills" %>
<%= f.collection_check_boxes :skill_ids, Skill.all, :id, :title %>
</div>
When I didn't have this setup, but only used belongs_to
and has_many
it worked, but now I'm getting an error that says: uninitialized constant Course::CourseSkill
and points me to the line where the collection_check_boxes
are. So what am I doing wrong? Also I was folloing this previous post if that makes my point more clear: How to add multiple many-to-many fields in one form with Rails 4?
As OP agreed in the comment, with my suggestion in the comment , I am taking it into the answer -
Your model name should be CourseSkill
, not CourseSkills
. As per the Rails convention has_many :course_skills
, translated to the model name as CourseSkill
..Look the doc The name of the other model is pluralized when declaring a has_many association. . Now read it reverse way..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.