根据关联 table 中的条件从 table 中获取记录
Fetch records from a table based on the condition in associated table
我有两个模型 School
和 Expense
,其中学校有很多费用,Expense
有一个名为 cost
的属性
我试图只获取那些费用成本大于某个值的学校,比方说 1000,我试过这个查询
School.includes(:expenses).where("lower(schools.name) LIKE '%a%' and expenses.cost >= 1000").select('*').from('schools, expenses')
此查询的问题在于,如果一所学校有多个费用大于 1000 的费用,它会返回重复值,我想要的只是基于条件的唯一学校。
请帮忙!
School.
joins(:expenses).
where("lower(schools.name) LIKE ?", "%a%"). # part of the code you shared
where("expenses.cost > ?", 1_000).
group("schools.id")
我有两个模型 School
和 Expense
,其中学校有很多费用,Expense
有一个名为 cost
我试图只获取那些费用成本大于某个值的学校,比方说 1000,我试过这个查询
School.includes(:expenses).where("lower(schools.name) LIKE '%a%' and expenses.cost >= 1000").select('*').from('schools, expenses')
此查询的问题在于,如果一所学校有多个费用大于 1000 的费用,它会返回重复值,我想要的只是基于条件的唯一学校。
请帮忙!
School.
joins(:expenses).
where("lower(schools.name) LIKE ?", "%a%"). # part of the code you shared
where("expenses.cost > ?", 1_000).
group("schools.id")