instance method in_order_of

Ruby on Rails 7.0.10

Since v7.0.10

Available in: v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

in_order_of(key, series)

Returns a new Array where the order has been set to that provided in the series, based on the key of the objects in the original enumerable.

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

If the series include keys that have no corresponding element in the Enumerable, these are ignored. If the Enumerable has additional elements that aren’t named in the series, these are not included in the result.

Parameters

key req
series req
Source
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 252
  def in_order_of(key, series)
    group_by(&key).values_at(*series).flatten(1).compact
  end

Defined in activesupport/lib/active_support/core_ext/enumerable.rb line 252 · View on GitHub · Improve this page · Find usages on GitHub

Defined in Enumerable

Type at least 2 characters to search.

↑↓ navigate · open · esc close