Rails ActiveRecord is continuously being improved. The latest improvement is the “in_order_of” method. This method is used to order records in the order of the given values (of a particular column). This query previously only used to allow symbolic column names, it now handles string values also.
Before
Let’s create a Rocket model which has id and name attributes. Now, when trying to order the records in the order of the given values, we can use the “in_order_of” method as shown below.
However if we try to use the “in_order_of” method with string values, it throws an error.
As you can see, the error is thrown because the “in_order_of” method is trying to call the “in” method on the string value “id”.
After
Thanks to this PR the ActiveRecord queries no longer check if a given column is symbol and then perform a query, but instead check for Arel types. This allows for greater flexibility.
What used to be,
activerecord/lib/active_record/relation/query_methods.rb
is now,
Now when we try to use the “in_order_of” method with string values, it works as expected.