module ActiveRecord::FinderMethods
def find(*args)
Person.where(name: 'Spartacus', rating: 4).pluck(:field1, :field2)
# returns an Array of ids.
Person.where(name: 'Spartacus', rating: 4).ids
# returns a chainable list of instances with only the mentioned fields.
Person.where(name: 'Spartacus', rating: 4).select("field1, field2, field3")
# returns a boolean indicating if any record with the given conditions exist.
Person.where(name: 'Spartacus', rating: 4).exists?(conditions = :none)
==== Alternatives for #find
# returns the first item or creates it and returns it.
Person.find_or_create_by(name: 'Spartacus', rating: 4)
# returns the first item or returns a new instance (requires you call .save to persist against the database).
Person.find_or_initialize_by(name: 'Spartacus', rating: 4)
# returns the first item or nil.
Person.find_by(name: 'Spartacus', rating: 4)
# returns a chainable list (which can be empty).
Person.where(name: 'Spartacus', rating: 4)
==== Variations of #find
end
person.save!
person.visits += 1
person = Person.lock(true).find(1)
Person.transaction do
expected person.visits == 4.
transaction has to wait until the first is finished; we get the
in two saves of person.visits = 3. By locking the row, the second
each will read person.visits == 2, add 1 to it, and save, resulting
Example for find with a lock: Imagine two concurrent transactions:
==== Find with lock
But ActiveRecord::QueryMethods#where method doesn't raise ActiveRecord::RecordNotFound.
method and provide an explicit ActiveRecord::QueryMethods#order option.
If you want the results to be sorted by database, you can use ActiveRecord::QueryMethods#where
NOTE: The returned records are in the same order as the ids you provide.
]
#
#
=> [
TravelRoute.find([["Berlin", "London"], ["Barcelona", "Lisbon"]])
]
#
#
=> [
TravelRoute.find(["New York", "Las Vegas"], ["New York", "Portland"])
=> [#
TravelRoute.find([["Paris", "Montreal"]])
=> #
TravelRoute.find(["Ottawa", "London"])
TravelRoute.primary_key = [:origin, :destination]
==== Find a record for a composite primary key model
Person.where("administrator = 1").order("created_on DESC").find(1)
Person.find([1]) # returns an array for the object with ID = 1
Person.find([7, 17]) # returns an array for objects with IDs in (7, 17), or with composite primary key [7, 17]
Person.find(1, 2, 6) # returns an array for objects with IDs in (1, 2, 6)
Person.find("31-sarah") # returns the object for ID = 31
Person.find("1") # returns the object for ID = 1
Person.find(1) # returns the object for ID = 1
If the primary key is an integer, find by id coerces its arguments by using +to_i+.
If one or more records cannot be found for the requested ids, then ActiveRecord::RecordNotFound will be raised.
and for models with a composite primary key, it will be an array of values.
`ID` refers to an "identifier". For models with a single-column primary key, `ID` will be a single value,
Find by id - This can either be a specific id (ID), a list of ids (ID, ID, ID), or an array of ids ([ID, ID, ID]).
def find(*args) return super if block_given? find_with_ids(*args) end