module PgORM::Query::Methods(T)
Direct including types
Defined in:
pg-orm/query/methods.crInstance Method Summary
-
#all : self
Query all records from the database.
-
#average(column_name : Symbol | String) : Float64
Calculates the average of a column.
-
#count(column_name : Symbol | String = "*", distinct = builder.distinct?) : Int64
Counts how many records match the SQL query.
-
#delete_all : Nil
Executes a DELETE SQL query.
-
#distinct(value = true) : self
Specify a DISTINCT statement for the query.
- #dup(builder : Builder)
-
#exists?(id : T::PrimaryKeyType) : Bool
Returns true when a record identified by primary key exists in the database with the current conditions.
-
#exists? : Bool
Returns true if the SQL query has at least one result.
-
#find(id : T::PrimaryKeyType) : T
Loads a record by id from the database.
-
#find?(id : T::PrimaryKeyType) : T | Nil
Same as
#find
but returnsnil
when the record doesn't exist. -
#find_by(**args) : T
Loads a record by arguments from the database.
-
#find_by?(**args) : T | Nil
Same as
#find_by
but returnsnil
when no record could be found in the database. -
#first : T
Loads the first record from the database, ordering by the primary key in ascending order unless an order has been specified.
-
#first? : T | Nil
Same as
#first?
but returnsnil
when no record could be found in the database. - #group_by(*columns : Symbol | String) : self
-
#ids : Array(T::PrimaryKeyType)
Loads all primary key values of rows matching the SQL query.
- #join(type : JoinType, model : Base.class, fk : Symbol, pk : Base.class | Nil = nil) : self
- #join(type : JoinType, model : Base.class, on : String) : self
-
#last : T
Loads the last record from the database, ordering by the primary key in ascending order unless an order has been specified.
-
#last? : T | Nil
Same as
#last?
but returnsnil
when no record could be found in the database. -
#limit(value : Int32) : self
Specify a LIMIT for the query.
-
#maximum(column_name : Symbol | String)
Returns the maximum value for a column.
-
#minimum(column_name : Symbol | String)
Returns the minimum value for a column.
-
#none : self
Ensures that the query will never return anything from the database.
-
#offset(value : Int32) : self
Specify an OFFSET for the query.
-
#order(columns : Hash(Symbol, Symbol)) : self
Specify an ORDER for the query.
-
#order(*columns : Symbol | String) : self
Specify an ORDER column for the query.
-
#order(**columns) : self
Specify an ORDER for the query.
-
#pluck(column_name : Symbol | String) : Array(Value)
Loads values of a single column as an Array.
-
#reorder(columns : Hash(Symbol, Symbol)) : self
Specify an ORDER for the query, replacing any previous ORDER definition.
-
#reorder(*columns : Symbol | String) : self
Specify an ORDER column for the query, replacing any previous ORDER definition.
-
#reorder(**columns) : self
Specify an ORDER for the query, replacing any previous ORDER definition.
-
#select(sql : String) : self
Specify a raw SELECT statement for the query.
-
#select(*columns : Symbol) : self
Specify SELECT columns for the query.
-
#size : Int64
Returns how many records match the SQL query.
-
#sum(column_name : Symbol | String) : Int64 | Float64
Calculates the sum of a column.
-
#take : T
Loads one record from the database, without any ordering.
-
#take? : T | Nil
Same as
#take
but returnsnil
when no record could be found in the database. -
#to_sql : String
Returns the generated SQL query.
-
#unscope(*args) : self
Resets previously set SQL statement(s).
-
#update_all(attributes : Hash | NamedTuple) : Nil
Executes an UPDATE SQL query.
-
#update_all(**attributes) : Nil
Executes an UPDATE SQL query.
-
#where(conditions : Hash(Symbol, Value | Array(Value)) | NamedTuple) : self
Specify WHERE conditions for the query.
-
#where(sql : String, *args : Value) : self
Specify a raw WHERE condition for the query.
-
#where(**conditions) : self
Specify WHERE conditions for the query.
- #where_not(conditions : Hash(Symbol, Value | Array(Value)) | NamedTuple) : self
- #where_not(**conditions) : self
Instance Method Detail
Query all records from the database. Doesn't actually issue any SQL query.
See Cache#to_a
to load all records from the database into an Array.
Calculates the average of a column. See #sum
for details.
Counts how many records match the SQL query.
You can count all columns or a specific column::
User.count
User.count(:name)
You can specify a raw SQL query with a String:
User.count("LENGTH(name)")
Executes a DELETE SQL query.
User.where(group_id: 1).delete_all
# => DELETE FROM "users" WHERE "group_id" = 1;
Returns true when a record identified by primary key exists in the database with the current conditions.
User.where(group_id: 1).exists?(2)
# => SELECT 1 AS one FROM "users" WHERE "group_id" = 1 AND "id" = 2 LIMIT 1;
Returns true if the SQL query has at least one result.
User.where(group_id: 1).exists?
# => SELECT 1 AS one FROM "users" WHERE "group_id" = 1 LIMIT 1;
Loads a record by id from the database. Raises a Error::RecordNotFound
exception when the record doesn't exist.
user = User.find(1)
# => SELECT * FROM "users" WHERE "id" = 1 LIMIT 1;
Same as #find
but returns nil
when the record doesn't exist.
Loads a record by arguments from the database. Raises a RecordNotFound
exception when the record doesn't exist. For example:
user = User.find_by(name: "user", group_id: 2)
# => SELECT * FROM "users" WHERE "name" = 'user' AND "group_id" = 2 LIMIT 1;
See #where
for more details on conditions.
Same as #find_by
but returns nil
when no record could be found in the
database.
Loads the first record from the database, ordering by the primary key in ascending order unless an order has been specified.
Merely takes the last entry in the cached result set if the relation was previously loaded.
Prefer #take
if you don't need an ordering or already specified one.
user = User.first
# => SELECT * FROM "users" ORDER BY "id" ASC LIMIT 1;
user = User.order(name: :desc).last
# => SELECT * FROM "users" ORDER BY "name" DESC LIMIT 1;
user = User.order("name ASC, group_id DESC").last
# => SELECT * FROM "users" ORDER BY name ASC, group_id DESC LIMIT 1;
Same as #first?
but returns nil
when no record could be found in the
database.
Loads the last record from the database, ordering by the primary key in ascending order unless an order has been specified.
Merely takes the last entry in the cached result set if the relation was previously loaded.
Prefer #take
if you don't need an ordering or already specified one.
user = User.last
# => SELECT * FROM "users" ORDER BY "id" DESC LIMIT 1;
user = User.order(name: :desc).last
# => SELECT * FROM "users" ORDER BY "name" ASC LIMIT 1;
user = User.order("name ASC, group_id DESC").last
# => SELECT * FROM "users" ORDER BY name DESC, group_id ASC LIMIT 1;
Returns the maximum value for a column. See #sum
for details.
Returns the minimum value for a column. See #sum
for details.
Specify an ORDER for the query. This is added to any previous order definition. For example:
User.order({name: :asc, group_id: :desc})
# => SELECT * FROM "users" ORDER BY "name" ASC, "group_id" DESC;
Specify an ORDER column for the query. This is added to any previous order definition. For example:
User.order(:name)
# => SELECT * FROM "users" ORDER BY "name" ASC;
Specify an ORDER for the query. This is added to any previous order definition. For example:
User.order(name: :asc, group_id: :desc)
# => SELECT * FROM "users" ORDER BY "name" ASC, "group_id" DESC;
Loads values of a single column as an Array.
names = User.pluck(:name)
# => SELECT "name" FROM "users";
# => ["user", "alice", ...]
Specify an ORDER for the query, replacing any previous ORDER definition.
See #order
for details.
Specify an ORDER column for the query, replacing any previous ORDER
definition. See #order
for details.
Specify an ORDER for the query, replacing any previous ORDER definition.
See #order
for details.
Returns how many records match the SQL query. Uses the cached result set if the query was previously loaded, otherwise executes a COUNT SQL query.
Calculates the sum of a column.
You can specify a column name:
User.sum(:salary)
You can specify a raw SQL query with a String:
User.sum("LENGTH(name)")
Loads one record from the database, without any ordering. Raises a
Error::RecordNotFound
exception when no record could be found.
user = User.take
# => SELECT * FROM "users" LIMIT 1;
Resets previously set SQL statement(s). For example:
users = User.where(group_id: 1).limit(10)
users.unscope(:limit) # == User.where(group_id: 1)
users.unscope(:where) # == User.limit(10)
users.unscope(:where, :limit) # == User.all
Available properties:
:select
:where
:order
:limit
:offset
Executes an UPDATE SQL query.
User.where(id: 1).update_all({group_id: 2})
# => UPDATE "users" SET "group_id" = 2 WHERE "id" = 1;
Executes an UPDATE SQL query.
User.where(id: 1).update_all(group_id: 2)
# => UPDATE "users" SET "group_id" = 2 WHERE "id" = 1;
Specify WHERE conditions for the query. For example:
conditions = {
:name => "user",
:group_id => 2,
}
User.where(conditions)
# => SELECT * FROM "users" WHERE "name" = 'user' AND "group_id" = 2;
The condition value may be nil:
User.where({:group_id => nil})
# => SELECT * FROM "users" WHERE "group_id" IS NULL;
The condition value may also be an Array of values:
User.where({:group_id => [1, 2, 3]})
# => SELECT * FROM "users" WHERE "group_id" IN (1, 2, 3);
Specify a raw WHERE condition for the query. You can specify arguments as
?
and pass them to the method. For example:
User.where("LENGTH(name) > ?", 10)
# => SELECT * FROM "users" WHERE LENGTH(name) > 10;
Specify WHERE conditions for the query. For example:
User.where(name: "user", group_id: 2)
# => SELECT * FROM "users" WHERE "name" = 'user' AND "group_id" = 2;
The condition value may be nil:
User.where(group_id: nil)
# => SELECT * FROM "users" WHERE "group_id" IS NULL;
The condition value may also be an Array of values:
User.where(group_id: [1, 2, 3])
# => SELECT * FROM "users" WHERE "group_id" IN (1, 2, 3);