Returns a cache key that can be used to identify the records fetched by
this query. The cache key is built with a fingerprint of the sql query, the
number of records matched by the query and a timestamp of the last updated
record. When a new record comes to match the query, or any of the existing
records is updated or deleted, the cache key changes.

Deletes the row with a primary key matching the id argument,
using a SQL DELETE statement, and returns the number of rows
deleted. Active Record objects are not instantiated, so the object's
callbacks are not executed, including any :dependent
association options.

Note: Although it is often much faster than the alternative, destroy, skipping callbacks might
bypass business logic in your application that ensures referential
integrity or performs other essential jobs.

Deletes the records without instantiating the records first, and hence not
calling the #destroy method
nor invoking callbacks. This is a single SQL DELETE statement that goes
straight to the database, much more efficient than destroy_all. Be careful with
relations though, in particular :dependent rules defined on
associations are not honored. Returns the number of rows affected.

Both calls delete the affected posts all at once with a single DELETE
statement. If you need to destroy dependent associations or call your
before_* or after_destroy callbacks, use the destroy_all method instead.

If an invalid method is supplied, delete_all raises an
ActiveRecordError:

Destroy an object (or multiple objects) that has the given id. The object
is instantiated first, therefore all callbacks and filters are fired off
before the object is deleted. This method is less efficient than delete but allows cleanup methods
and other actions to be run.

This essentially finds the object (or multiple objects) with the given id,
creates a new object from the attributes, and then calls destroy on it.

Destroys the records by instantiating each record and calling its #destroy method. Each
object's callbacks are executed (including :dependent
association options). Returns the collection of objects that were
destroyed; each will be frozen, to reflect that no changes should be made
(since they can't be persisted).

Note: Instantiation, callback execution, and deletion of each record can be
time consuming when you're removing many records at once. It generates
at least one SQL DELETE query per record (or possibly more, to
enforce your callbacks). If you want to delete many rows quickly, without
concern for their associations or callbacks, use delete_all instead.

Examples

# File activerecord/lib/active_record/relation.rb, line 459defdestroy_all(conditions = nil)
ifconditionsActiveSupport::Deprecation.warn(" Passing conditions to destroy_all is deprecated and will be removed in Rails 5.1.
To achieve the same use where(conditions).destroy_all.
".squish)
where(conditions).destroy_allelserecords.each(&:destroy).tap { reset }
endend

Finds the first record with the given attributes, or creates a record with
the attributes if one is not found:

# Find the first user named "Penélope" or create a new one.
User.find_or_create_by(first_name: 'Penélope')
# => #<User id: 1, first_name: "Penélope", last_name: nil>
# Find the first user named "Penélope" or create a new one.
# We already have one so the existing record will be returned.
User.find_or_create_by(first_name: 'Penélope')
# => #<User id: 1, first_name: "Penélope", last_name: nil>
# Find the first user named "Scarlett" or create a new one with
# a particular last name.
User.create_with(last_name: 'Johansson').find_or_create_by(first_name: 'Scarlett')
# => #<User id: 2, first_name: "Scarlett", last_name: "Johansson">

This method accepts a block, which is passed down to create. The last example above can
be alternatively written this way:

# Find the first user named "Scarlett" or create a new one with a
# different last name.
User.find_or_create_by(first_name: 'Scarlett') do |user|
user.last_name = 'Johansson'
end
# => #<User id: 2, first_name: "Scarlett", last_name: "Johansson">

This method always returns a record, but if creation was attempted and
failed due to validation errors it won't be persisted, you get what create returns in such situation.

Please note *this method is not atomic*, it runs first a SELECT, and if
there are no results an INSERT is attempted. If there are other threads or
processes there is a race condition between both calls and it could be the
case that you end up with two similar records.

Whether that is a problem or not depends on the logic of the application,
but in the particular case in which rows have a UNIQUE constraint an
exception may be raised, just retry:

# File activerecord/lib/active_record/relation.rb, line 33definitialize_copy(other)
# This method is a hot spot, so for now, use Hash[] to dup the hash.# https://bugs.ruby-lang.org/issues/7166@values = Hash[@values]
resetend

Joins that are also marked for preloading. In which case we should just
eager load them. Note that this is a naive implementation because we could
have strings and symbols which represent the same association, but that
aren't matched by this. Also, we could have nested hashes which
partially match, e.g. { a: :b } & { a: [:b, :c] }

Causes the records to be loaded from the database if they have not been
loaded already. You can use this if for some reason you need to explicitly
load some records before actually using them. The return value is the
relation itself, not the records.

Note: Updating a large number of records will run an UPDATE query for each
record, which may cause a performance issue. So if it is not needed to run
callbacks for each update, it is preferred to use update_all for updating all
records using a single query.

Updates all records in the current relation with details given. This method
constructs a single SQL UPDATE statement and sends it straight to the
database. It does not instantiate the involved models and it does not
trigger Active Record callbacks or validations. However, values passed to
update_all will still go
through Active Record's normal type casting and serialization.

Parameters

updates - A string, array, or hash representing the SET part
of an SQL statement.

Examples

# Update all customers with the given attributes
Customer.update_all wants_email: true
# Update all books with 'Rails' in their title
Book.where('title LIKE ?', '%Rails%').update_all(author: 'David')
# Update all books that match conditions, but limit it to 5 ordered by date
Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(author: 'David')
# Update all invoices and set the number column to its id value.
Invoice.update_all('number = id')