如何在ActiveRecord中查询对象数组



我正在使用ActiveRecord,在那里我试图查询课程模型。每门课程都有以下模式:

Courses: 
[{
name: "Intro to Math",
id: 1,
segments: [{id: 1, subject_id: "123-456"}, {id: 1, subject_id: "789-145"}]
},
{
name: "Intro to Probability",
id: 2,
segments: [{id: 1, subject_id: "123-456"}, {id: 1, subject_id: "000-000"}]
},
{
name: "Intro to Stats",
id: 3,
segments: [{id: 1, subject_id: "111-111"}, {id: 1, subject_id: "000-000"}]
}]

我该如何编写一个ActiveRecord查询,以获取每个具有id为"123-456"的段的课程(在本例中,它将是id为1&2的课程(?

Course.joins(:segments).where(segments: { subject_id: "123-456" })

有关更多信息和示例,请参阅文档。

根据这句话:Course which has a segment which has the id "123-456" (which in this case, would be the Course with the id 1 & 2),你的意思似乎是Course which has a segment which has the segment_id "123-456"如果是这样的话:

Courses.joins(:segments).where("segments.subject_id='123-456'")