Today I Learned

hashrocket A Hashrocket project

ActiveRecord find with array is an ordered return

You probably already know that passing an Array to an ActiveRecord .find will do an IN query and return the results.

User.find([1,2,37])

=> [<User id: 1>, <User id: 2>, <User id: 37>]

But did you know that the order of the array affects the returned result order? This is an order not done at the database level.

User.find([37,2,1])

=> [<User id: 37>, <User id: 2>, <User id: 1>]
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.