Rails 7 has
a method called in_order_of in enumerable that returns a new array ordered according
to the provided series.
Order series should be based on the key of the objects in the original enumerable.
Let’s say we have a User struct with id and name attributes.
If we want to get the users in order of ids as [6, 1, 4],
we would have to add a custom method as shown below
With the changes in Rails 7,
we can replace the above custom code with in_order_of as below
Similarly, we can order on basis of name attribute as well.
If the series include values that have no corresponding record in the Enumerable,
they are ignored and, the rest will be returned.
If the Enumerable has additional elements that aren’t included
in the series then those will be ignored in the result.