Proposal: Option `filter` to `in_order_of` method

As explained in #51761, the in_order_of method (ActiveRecord and ActiveSupport) currently always filters the collection results only with the values specified in the array determined for ordering. In some cases, we just want the collection to be sorted primarily with the defined values without applying any filters, and we don’t care about the rest of the ordering.

When looking for a solution for this case, cause I missed this feature for my own project, I came across other people asking for something similar as well. As a result, i opened PRs #51761 and #52072 (ActiveRecord and ActiveSupport, respectively) which adds the filter option to define whether we want to use the filter or not.

Below is an example of usage:

# Enumerable
[ Person.find(5), Person.find(3), Person.find(1) ].in_order_of(:id, [ 1, 5 ], filter: false)
# => [ Person.find(1), Person.find(5), Person.find(3) ]

# ActiveRecord::Relation
Post.in_order_of(:id, order, filter: false).to_sql
# SELECT
 # "posts".* FROM "posts"
# ORDER BY
 # CASE WHEN "posts"."id" = 3 THEN 1 WHEN "posts"."id" = 4 THEN 2 WHEN "posts"."id" = 1 THEN 3 ELSE 4 END ASC

Please share your opinions on whether you think this is a valid idea or not. Thanks anyway!

2 Likes

This feature is very useful. Looking forward to see it merged.

1 Like