Method: ActiveModel::Datastore::ClassMethods#find

Defined in:
lib/active_model/datastore.rb

#find(*ids, parent: nil) ⇒ Model, ...

Find entity by id - this can either be a specific id (1), a list of ids (1, 5, 6), or an array of ids ([5, 6, 10]). The parent key is optional.

Parameters:

  • ids (Integer)

    One or more ids to retrieve.

  • parent (Google::Cloud::Datastore::Key) (defaults to: nil)

    The parent key of the entity.

Returns:

  • (Model, nil)

    An ActiveModel object or nil for a single id.

  • (Array<Model>)

    An array of ActiveModel objects for more than one id.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/active_model/datastore.rb', line 311

def find(*ids, parent: nil)
  expects_array = ids.first.is_a?(Array)
  ids = ids.flatten.compact.uniq.map(&:to_i)

  case ids.size
  when 0
    raise EntityError, "Couldn't find #{name} without an ID"
  when 1
    entity = find_entity(ids.first, parent)
    model_entity = from_entity(entity)
    expects_array ? [model_entity].compact : model_entity
  else
    lookup_results = find_all_entities(ids, parent)
    from_entities(lookup_results.all)
  end
end