---
title: "Rails 7 adds Enumerable#in_order_of"
description: "Rails 7.0 adds Enumerable#in_order_of"
canonical_url: "https://www.bigbinary.com/blog/rails-7-adds-enumerable-in-order-of"
markdown_url: "https://www.bigbinary.com/blog/rails-7-adds-enumerable-in-order-of.md"
---

# Rails 7 adds Enumerable#in_order_of

Rails 7.0 adds Enumerable#in_order_of

- Author: Ashik Salman
- Published: March 23, 2021
- Categories: Rails, Rails 7

Rails 7 introduces the `Enumerable#in_order_of` method, by which we can order
and constrain any enumerable collection using a key-series pair.

```ruby
=> Item = Struct.new(:price)
=> items = [Item.new(24), Item.new(32), Item.new(16)]

=> items.in_order_of(:price, [16, 32, 24])
=> [#<struct Item price=16>, #<struct Item price=32>, #<struct Item price=24>]
```

If any value in the series has no associated records in the enumerable
collection, it will be ignored and the rest will be returned in the result.

```ruby
=> items.in_order_of(:price, (15..25))
=> [#<struct Item price=16>, #<struct Item price=24>]
```

Similarly, any values not included in the series will be omitted from the final
result.

```ruby
=> items.in_order_of(:price, [16, 32])
=> [#<struct Item price=16>, #<struct Item price=32>]
```

Check out this [pull request](https://github.com/rails/rails/pull/41333) for
more details.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-7-adds-enumerable-in-order-of)
