简体   繁体   中英

Getting data from many to many relation with rails 4

I am developing a rails app with mysql database.

Theres bunch of tables but for this task I am using only 3. Students, Courses and CoursesPerStudent.

I have models for students and courses(no model for the third). The tables are already populated.

The tables:

students - id,name,family,address,phone,email,status,path
courses - courseCode, courseName, hours
coursesperstudent - code, student(same ids as in students table ids but not a oreign key),path,course(courses table code(foreign key)),examMark.

Now the thing is i need to show for each student a list of the courses realted to him.

How should i do it? Should i create model for the third table? how should i call it? and combine the two other tables in one to show specific courses?

Or there some other way?

Hopefully someone will help because im out of answers.

If you make right has_and_belongs_to_many association you can call:

Student.find(id).courses  

it return collection of courses

http://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM