mercredi 17 juillet 2019

get record from many to many relationship

I am working on an old rails project. I have a many-to-many relationship between Student and Course via a bridge model named students_courses.

Student class:

class Student < ActiveRecord::Base
  has_and_belongs_to_many :courses, :class_name => 'Course', :join_table => 'students_courses'
end

Course class:

class Course < ActiveRecord::Base

end

StudentCourse class:

class StudentCourse < ActiveRecord::Base
  belongs_to :student
  belongs_to :course
end

There is a firstname attribute in Student model. I would like to find the student who has firstname "John" and studying course "foo". How can I get this in ruby of Rails?

Aucun commentaire:

Enregistrer un commentaire