class DBX::ORM::ModelQuery(Model)
- DBX::ORM::ModelQuery(Model)
- DBX::Query
- Reference
- Object
Overview
Generic ModelQuery
class.
Automatically injected into the models.
If you want to customize the queries of a model, you can define
you own ModelQuery
into this model.
class User < DBX::ORM::Model
# ...
class ModelQuery < DBX::ORM::ModelQuery(User)
# A custom `select`
def select_custom
self.select({:id, :name, :about, :age})
end
end
end
In the model example above, we have added a new method (select_custom
) to `ModelQuery',
which can be used in each query.
user = User.find(id).select_custom.to_o
users = User.find.select_custom.to_a
Defined in:
orm/model_query.crConstructors
Instance Method Summary
-
#create!(data, returning : DBX::QueryBuilder::OneOrMoreFieldsType = "*")
Creates a new record and returns.
-
#query_all
Executes current query using current
Model::Schema
. -
#query_one
Executes current query using current
Model::Schema
. -
#query_one!
Executes current query using current
Model::Schema
. -
#rel(path : Symbol | String, table_alias : String | Symbol | Nil = nil) : ModelQuery(Model)
Refers to the result of a join in a defined
relation
property path. -
#select_all(model_class : DBX::ORM::Model.class, table_alias : String | Symbol | Nil = nil) : ModelQuery(Model)
Selects the relation fields.
-
#select_all : ModelQuery(Model)
Selects all SQL fields.
-
#selected_all?(model_class : DBX::ORM::Model.class, table_alias : String | Symbol | Nil = nil) : Bool
Returns
true
if the relation fields are selected,false
otherwise. -
#selected_all? : Bool
Returns
true
if the model fields are selected,false
otherwise. -
#to_a(as types)
Shortcut, same as
query_all(types)
. -
#to_a
Shortcut, same as
#query_all
. -
#to_o(as types)
Shortcut, same as
query_one(types)
. -
#to_o
Shortcut, same as
#query_one
. -
#to_o!(as types)
Shortcut, same as
query_one!(types)
. -
#to_o!
Shortcut, same as
#query_one!
.
Instance methods inherited from class DBX::Query
build : Tuple
build,
builder : DBX::QueryBuilder
builder,
create!(data : Hash | NamedTuple, as types, returning : DBX::QueryBuilder::OneOrMoreFieldsType = "*", pk_name : DBX::QueryBuilder::FieldType = :id, pk_type = ::Union(Int64, ::Nil))
create!,
exec
exec,
exec!
exec!,
queryquery(&) query, query_all(as types)
query_all(&) query_all, query_each(&) query_each, query_one(as types)
query_one(&) query_one, query_one!(as types)
query_one!(&) query_one!, raw_query(&) : Query raw_query, scalar scalar, scalar! scalar!, to_a(as types)
to_a(&) to_a, to_o(as types)
to_o(&) to_o, to_o!(as types)
to_o!(&) to_o!
Constructor methods inherited from class DBX::Query
new(adapter : DBX::Adapter::Base)
new
Constructor Detail
Instance Method Detail
Creates a new record and returns.
test = Test.create!(data)
puts test.id
Refers to the result of a join in a defined relation
property path.
users = User
.find
.rel("groups")
.left_join("groups", "groups.id", "users.group_id")
.to_a
Selects the relation fields. This method is automatically called by the methods related to the joins with a model class.
Returns true
if the relation fields are selected, false
otherwise.
See select_rel_fields
Returns true
if the model fields are selected, false
otherwise.
See #select_all