简体   繁体   English

如何使用Ruby on Rails上的Join将两个或多个参数传递给两个不同的表

[英]How do i pass two or more parameters to two different tables using Joins on Ruby on rails

i have the exams table, the subjects table, and the exam_subjects table. 我有考试表,科目表和exam_subjects表。 i want to filter lets say by Exam.id and Subject.id on the WHERE how do i pass those parameters. 我想过滤,通过Exam.id和Subject.id在WHERE上说如何传递这些参数。

@exams = Exam.joins(exam_subjects: :subject).where(:exam_id => params[:exam_id], :subject_id => params[:subject_id])

Have you tried something like 你尝试过类似的东西吗

@exams = Exam.joins(exam_subjects: :subject).where(:id => [params[:exam_id], params[:subject_id]])

?

You can use an string as a parameter in the where condition: 您可以在where条件中使用字符串作为参数:

@exams = Exam.joins(exam_subjects: :subject).where('id = ? AND exam_id = ?', params[:exam_id], params[:subject_id])

Take a look at Pure String Conditions on active record. 查看活动记录上的“ 纯字符串条件 ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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