Joaquín
Joaquín

Reputation: 350

undefined local variable or method `student_courses' for #<Class:0x000055ee7729d680> after creating a model

i created a model called Student Courses and then i migrate the database,the page was working well before doing that and i was trying to create a has_many model for my databasei created a model called StudentCourses and then i migrated the database, before thoing that the page were working

i used Scaffold to create Student with name, last name and email, the same for courses but courses only have a name for it

Student:

class Student < ApplicationRecord
    has_many :student_courses
    has_many :courses, through: student_courses
end

Course:

class Course < ApplicationRecord
    has_many :student_courses
    has_many :students, through: student_courses
end

this is my StudentCourse model:

class StudentCourse < ApplicationRecord
  belongs_to :student
  belongs_to :course
end

the problem must be in my Student controller?

Upvotes: 0

Views: 55

Answers (1)

arieljuod
arieljuod

Reputation: 15838

The association is configured using a symbol

has_many :courses, through: :student_courses

note the ":" before "student_courses"

Upvotes: 2

Related Questions