Clear::Model::CollectionBase(T)
Inherits Clear::SQL::SelectBuilder < Clear::SQL::Query::WithPagination < Clear::SQL::Query::BeforeQuery < Clear::SQL::Query::Change < Clear::SQL::Query::Connection < Clear::SQL::Query::Pluck < Clear::SQL::Query::Fetch < Clear::SQL::Query::Execute < Clear::SQL::Query::Lock < Clear::SQL::Query::Window < Clear::SQL::Query::CTE < Clear::SQL::Query::Aggregate < Clear::SQL::Query::OffsetLimit < Clear::SQL::Query::GroupBy < Clear::SQL::Query::OrderBy < Clear::SQL::Query::Having < Clear::SQL::Query::Where < Clear::SQL::Query::Join < Clear::SQL::Query::From < Clear::SQL::Query::Select < Enumerable < Reference < Object
CollectionBase(T) is the base class for collection of model.
Collection of model are a SQL SELECT query mapping & building system. They are Enumerable and are
`Clear::SQL::SelectBuilder` behavior; therefore, they can be used array-like and are working with low-level SQL
Building.
The CollectionBase(T) is extended by each model. For example, generating the model MyModel will generate the
class MyModel::Collection which inherits from CollectionBase(MyModel)
Collection are instantiated using Model.query method.
Class methods
Instance methods
Add an item to the current collection.
If the current collection is not originated from a has_many or has_many through: relation, calling << over
the collection will raise a Clear::SQL::OperationNotPermittedError
Returns self and therefore can be chained
Build a new collection; if the collection comes from a has_many relation
(e.g. my_model.associations.build), the foreign column which store
the primary key of my_model will be setup by default, preventing you
to forget it.
You can pass extra parameters using a named tuple:
my_model.associations.build({a_column: "value"})
Build a new collection; if the collection comes from a has_many relation
(e.g. my_model.associations.build), the foreign column which store
the primary key of my_model will be setup by default, preventing you
to forget it.
Use SQL COUNT over your query, and return this number as a Int64
Delete all the rows which would have been returned by this collection.
Is equivalent to collection.to_delete.execute
Duplicate the current request. Select query are mutable objects, and many of the methods will change the state of the collection:
collection = User.query # SELECT * FROM users;
collection.select("id") # SELECT id FROM users;
collection.select("id") # SELECT id, id FROM users;
Therefore, you may want to use dup to duplicate the current state of the collection.
Build the SQL, send the query then iterate through each models gathered by the request.
Build the SQL, send the query then iterate through each models gathered by the request. Use a postgres cursor to avoid memory bloating. Useful to fetch millions of rows at once.
A convenient way to write where({any_column: "any_value"}).first
A convenient way to write where({any_column: "any_value"}).first!
A convenient way to write where{ condition }.first!
Returns a model using primary key equality. Raises error if the model is not found.
Try to fetch a row. If not found, build a new object and setup the fields like setup in the condition tuple.
Try to fetch a row. If not found, build a new object and setup the fields like setup in the condition tuple. Just after building, save the object.
Try to fetch a row. If not found, build a new object and setup the fields like setup in the condition tuple. Just after building, save the object.
Get the first row from the collection query.
if not found, return nil
Get the first row from the collection query. if not found, throw an error
Get the last row from the collection query.
if not found, return nil
Get the last row from the collection query. if not found, throw an error
Build the SQL, send the query then build and array by applying the block transformation over it.
Unlink the model currently referenced through a relation has_many through
If the current colleciton doesn't come from a has_many through relation,
this method will throw Clear::SQL::OperationNotPermittedError
Returns true if unlinking is successful (e.g. one or more rows have been updated), or false otherwise