mysql - Query multiple tables with linked data -
overview:
i have rails project using mysql db has data being upload 2 sources college students grades.
when go upload grades, need first find "enroll" record section of course student enrolled in. "enroll" connected both "student" , "section" belongs_to. student exist in students table, section , enroll may or may not exist. if not exist want create them.
current code:
my current attempt solve is:
enrolled = enroll.find_or_create_by(student: student, section: {course: course, semester: semester, year_offered: year_taken})
i getting following error:
mysql2::error: unknown column 'section.semester' in 'where clause': select enrolls
.* enrolls
section
.semester
= 'fall' , section
.year_offered
= 2012 , section
.course_id
= 4 , enrolls
.student_id
= 11 limit 1
questions:
1) right way go query? need make multi-step in case section doesn't exist?
2) why getting unknown column error?
other details:
sections = section.where(course: course, semester: semester, year_offered: year_taken) works fine.
enroll belongs_to both student , section.
students , sections has_many enrolls relevant portion of model dependency diagram is:
i new rails, if there else need please let me know. or if there page explains already, please point me there. found hard find relevant pages not real familiar terminology when searching.
it looks didn't join section table in query. try this:
select enrolls.* enrolls inner join section on enrolls.belongs_to = section.belongs_to section.semester = 'fall' , section.year_offered = 2012 , section.course_id = 4 , enrolls.student_id = 11 limit 1
Comments
Post a Comment