The in_order_of
method allows developers to retrieve records in a specified order based on a given list of values.
It uses the where clause to filter the results only with the values specified in values.
It is particularly useful when the desired order is not strictly alphabetical or numerical but rather custom-defined.
The recent PR introduces the filter
option to the in_order_of
method, allowing for even more flexibility.
By default, filter
is set to true, which means only the specified values are included in the result. When filter
is set to false, all records are included, with the specified values prioritized.
Before
Prior to the implementation of this feature, the method returned only the filtered values.
Let’s take an example to understand the behavior better
Suppose we have an Invoice
model with the following records:
Now consider a scenario where we want to fetch invoices with sent
status first, followed by draft
invoices. The code for it will look like this:
Now the above code only returns the ids of the specified status and if we wanted to include all the status in the result then we would have to write custom logic for it.
After
With the filter
option, this becomes straightforward
The above code prioritizes the sent
and draft
status first and then included all the records.
To know more about this feature, please refer to this PR