This is a shortcut for .to_a.find { ... }
. The source code method is used here:
def find(*args) if block_given? to_a.find(*args) { |*block_args| yield(*block_args) } else find_with_ids(*args) end end
If you pass a block, it calls .to_a
(loads all records) and calls Enumerable#find
in the array.
In other words, it allows you to use Enumerable#find
on ActiveRecord::Relation
. This can be useful if your condition cannot be expressed or evaluated in SQL, for example. request for serialized attributes :
Consumer.find { |c| c.preferences[:foo] == :bar }
To avoid confusion, I would prefer a more explicit version:
Consumer.all.to_a.find { |c| c.preferences[:foo] == :bar }
source share