Take Rails: ActiveRecord query based on association value for example,
If we want to find reports with any related servers
' company_id
is 5
, we can write as
Report.includes(:servers).where(servers: {company_id: 5})
Given a report has multiple related servers, some server company_id is 5, some company_id is 6.
let's say,
report_a
has 2 servers, with company_id, 5,6 separatelyreport_b
has 2 servers, both with company_id, 5report_c
has 2 servers, both with company_id, 6
How to find reports with all related servers's company_id is 5?(only all related servers's company_id is 5, then the report is qualified)
in the above case, the report_b
is the answer
And find reports with all related servers's company_id is not 5?(only all related servers's company_id is Not 5, then the report is qualified)
in the above case, the report_c
is the answer
Can I achieve by use where
only?