Rails 6.1
adds
a query method missing to search for orphan records within ActiveRecord.
We often find ourselves querying an ActiveRecord model for orphan objects for reasons like cleanup or bulk updates.
Example
Let us consider the following models.
Before Rails 6.1
Now let us try to find all the job listings which do not have a manager assigned.
In the example above, we have used left_joins, but we could also have used includes or eager_load to achieve the same result.
After Rails 6.1
Rails 6.1
adds
a query method missing to the ActiveRecord::QueryMethods::WhereChain class.
It returns a new relation with a left outer join and where clause between the parent and child
models to identify missing relations.
Similarly from the example above, let us find all the job listings which do not have a manager assigned.
We can also combine multiple relations by passing in multiple relation names to the method.
For example, to find job listings which are missing both a manager and any job applications:
In the example above,
even though we have a job listing where a manager is not assigned,
an empty relation is returned because that job listing had job applications.
Here, this query identified job listings which had neither a manager,
nor any job applications.