class PgORM::Collection(T)

Overview

Build Database Queries.

A Collection is immutable: all methods will return a copy of the previous Collection with the added constraint(s). For example:

users = User.select(:id, :name).where(group_id: 2)

Termination methods such as #find_by, or #take will explicitly execute a SQL request against the database and load one record.

first_user = users.order(:name).take
# => SELECT "id", "name"
#    FROM "users"
#    WHERE "group_id" = 2
#    ORDER BY "name" ASC
#    LIMIT 1;

Termination methods such as #to_a or #each will execute a SQL request then cache loaded records into the Collection, so further accesses won't re-execute the SQL request. Some methods such as #first or #size will leverage this cache when it's available.

users.to_a
# => SELECT "id", "name" FROM "users" WHERE "group_id" = 2;

When specifying column names you should always use a Symbol, so they'll be properly quoted for the database server. In many cases you can specify raw SQL statements using a String. For example:

users = User.where("LENGTH(name) > $0", 10)
# => SELECT * FROM "users" WHERE LENGTH(name) > 10;

users = User.order("LENGTH(name) DESC")
# => SELECT * FROM "users" ORDER BY LENGTH(name) DESC;

count = User.count("LENGTH(name)", distinct: true)
# => SELECT COUNT(DISTINCT LENGTH(name)) FROM "users";

Included Modules

Defined in:

pg-orm/collection.cr

Instance Method Summary

Instance methods inherited from module PgORM::Query::Cache(T)

cached? cached?, each(& : T -> ) : Nil
each
each
, reload reload, to_a : Array(T) to_a

Instance methods inherited from module PgORM::Query::Methods(T)

all : self all, average(column_name : Symbol | String) : Float64 average, count(column_name : Symbol | String = "*", distinct = builder.distinct?) : Int64 count, delete_all : Nil delete_all, distinct(value = true) : self distinct, dup(builder : Builder) dup, exists?(id : T::PrimaryKeyType) : Bool
exists? : Bool
exists?
, find(id : T::PrimaryKeyType) : T find, find?(id : T::PrimaryKeyType) : T | Nil find?, find_by(**args) : T find_by, find_by?(**args) : T | Nil find_by?, first : T first, first? : T | Nil first?, group_by(*columns : Symbol | String) : self group_by, ids : Array(T::PrimaryKeyType) ids, join(type : JoinType, model : Base.class, fk : Symbol, pk : Base.class | Nil = nil) : self
join(type : JoinType, model : Base.class, on : String) : self
join
, last : T last, last? : T | Nil last?, limit(value : Int32) : self limit, maximum(column_name : Symbol | String) maximum, minimum(column_name : Symbol | String) minimum, none : self none, offset(value : Int32) : self offset, order(columns : Hash(Symbol, Symbol)) : self
order(*columns : Symbol | String) : self
order(**columns) : self
order
, pluck(column_name : Symbol | String) : Array(Value) pluck, reorder(columns : Hash(Symbol, Symbol)) : self
reorder(*columns : Symbol | String) : self
reorder(**columns) : self
reorder
, select(sql : String) : self
select(*columns : Symbol) : self
select
, size : Int64 size, sum(column_name : Symbol | String) : Int64 | Float64 sum, take : T take, take? : T | Nil take?, to_sql : String to_sql, unscope(*args) : self unscope, update_all(attributes : Hash | NamedTuple) : Nil
update_all(**attributes) : Nil
update_all
, where(conditions : Hash(Symbol, Value | Array(Value)) | NamedTuple) : self
where(sql : String, *args : Value) : self
where(**conditions) : self
where
, where_not(conditions : Hash(Symbol, Value | Array(Value)) | NamedTuple) : self
where_not(**conditions) : self
where_not

Instance Method Detail

def in_groups_of(size : Int, filled_up_with : U = nil, reuse = false, &) forall U #

Yields a block with the chunks in the given size.

[1, 2, 4].in_groups_of(2, 0) { |e| p e.sum }
# => 3
# => 4

By default, a new array is created and yielded for each group.

  • If reuse is given, the array can be reused
  • If reuse is an Array, this array will be reused
  • If reuse is truthy, the method will create a new array and reuse it.

This can be used to prevent many memory allocations when each slice of interest is to be used in a read-only fashion.


[View source]