class
CQL::ActiveRecord::Queryable::PartialBuilder(T)
- CQL::ActiveRecord::Queryable::PartialBuilder(T)
- CQL::ActiveRecord::Queryable::QueryBuilder(T)
- Reference
- Object
Overview
A builder after select(:a, :b): it keeps the whole chain API, but results
are T::Row records carrying only the selected columns.
Defined in:
active_record/query_builder.crConstructors
-
.new(query : CQL::Query, model_class : T.class = T)
Initialize a new QueryBuilder with the given query - @param query [CQL::Query] The underlying query object - @param model_class [T.class] The model class
Instance Method Summary
-
#all
Execute the query and return all results When preloads are configured, automatically applies them to the results.
-
#each(& : T::Row -> )
Iterate over each result - @yield [T] Each model instance
-
#first(as as_kind = T::Row)
Execute the query and return the first result - @return [T?] First model instance or nil
-
#first!(as as_kind = T::Row)
Execute the query and return the first result, raises if not found - @return [T] First model instance
-
#last(as as_kind = T::Row)
Execute the query and return the last result (using reverse order) - @return [T?] Last model instance or nil
-
#last!(as as_kind = T::Row)
Execute the query and return the last result, raises if not found - @return [T] Last model instance
-
#preload(*associations : Symbol) : self
Add associations to preload (eager load) to avoid N+1 queries.
Instance methods inherited from class CQL::ActiveRecord::Queryable::QueryBuilder(T)
all(as as_kind)all all, any? any?, average(column : Symbol) average, avg(column : Symbol) avg, count(column : Symbol = :*) count, count_query(column : Symbol = :*) : CQL::Query count_query, delete_all : Int64 delete_all, distinct distinct, distinct_values(column : Symbol, as as_kind = DB::Any) distinct_values, each(& : T -> ) each, empty? empty?, exists? : Bool
exists?(**conditions) exists?, find(id) find, find!(id) find!, find_by(conditions : Hash(String | Symbol, DB::Any | Array(DB::Any)))
find_by(**fields) find_by, find_by!(conditions : Hash(String | Symbol, DB::Any | Array(DB::Any)))
find_by!(**fields) find_by!, find_each(batch_size : Int32 = 1000, & : T -> ) find_each, find_in_batches(batch_size : Int32 = 1000, & : Array(T) -> ) find_in_batches, first(as as_kind = T) first, first!(as as_kind = T) first!, get(as as_kind) get, group(*columns : Symbol | String) group, group_by(*columns : Symbol | String) group_by, having(&) having, ids(as as_kind = Int64) ids, join(table_or_alias : Symbol)
join(table_or_alias : Symbol, &block : Expression::FilterBuilder -> Expression::ConditionBuilder)
join(**tables_with_aliases) join, joins(association : Symbol) : self
joins(association : Symbol, &) : self joins, last(as as_kind = T) last, last!(as as_kind = T) last!, left(table_or_alias : Symbol)
left(table_or_alias : Symbol, &block : Expression::FilterBuilder -> Expression::ConditionBuilder)
left(**tables_with_aliases) left, left_joins(association : Symbol) : self
left_joins(association : Symbol, &) : self left_joins, limit(value : Int32) limit, many? many?, max(column : Symbol) max, maximum(column : Symbol) maximum, merge(other : QueryBuilder(T)) merge, min(column : Symbol) min, minimum(column : Symbol) minimum, model_class : T.class model_class, none none, offset(value : Int32) offset, order(*fields : Symbol | String)
order(**fields) order, order_by(**fields : **N) : self forall N order_by, pick(column : Symbol, as as_kind = DB::Any) pick, pluck(*columns : Symbol, as as_kind) pluck, preload(*associations : Symbol) : self preload, query query, reorder(*fields : Symbol | String)
reorder(**fields) reorder, reverse_order reverse_order, right(table_or_alias : Symbol)
right(table_or_alias : Symbol, &block : Expression::FilterBuilder -> Expression::ConditionBuilder)
right(**tables_with_aliases) right, select(*columns : Symbol) : PartialBuilder(T)
select(*columns : Symbol | String)
select(**fields) select, size size, sum(column : Symbol) sum, to_a to_a, to_sql to_sql, to_sql_with_params to_sql_with_params, unscope(scope_type : Symbol) unscope, using(connection : DB::Connection) : self
using(executor : CQL::Executor) : self using, where(conditions : Hash(String | Symbol, DB::Any | Array(DB::Any)))
where(__sql sql : String, *positional, **named)
where(**fields)
where(&) : self where, where_gt(field : Symbol | String, value : DB::Any) where_gt, where_like(field : Symbol | String, pattern : String) where_like, where_lt(field : Symbol | String, value : DB::Any) where_lt, where_primary_key(key : DB::Any | UUID) : QueryBuilder(T)
where_primary_key(keys : Array) : QueryBuilder(T) where_primary_key, with_query(&block : CQL::Query -> CQL::Query) with_query
Constructor methods inherited from class CQL::ActiveRecord::Queryable::QueryBuilder(T)
new(query : CQL::Query, model_class : T.class = T)
new
Class methods inherited from class CQL::ActiveRecord::Queryable::QueryBuilder(T)
from_model(model_class : T.class) : QueryBuilder(T) forall T
from_model
Constructor Detail
Initialize a new QueryBuilder with the given query
- @param query [CQL::Query] The underlying query object
- @param model_class [T.class] The model class
Instance Method Detail
Execute the query and return all results When preloads are configured, automatically applies them to the results.
- @return [Array(T)] Array of model instances
Iterate over each result
- @yield [T] Each model instance
Execute the query and return the first result
- @return [T?] First model instance or nil
Execute the query and return the first result, raises if not found
- @return [T] First model instance
Execute the query and return the last result (using reverse order)
- @return [T?] Last model instance or nil
Execute the query and return the last result, raises if not found
- @return [T] Last model instance
Add associations to preload (eager load) to avoid N+1 queries. Preloaded associations are fetched in separate batch queries after the main query executes, then injected into the parent records.
- @param associations [Symbol*] The association names to preload
- @return [QueryBuilder(T)] A new query builder with preload configured
Example
User.preload(:posts).all # Preload an association
User.where(active: true).preload(:posts).all # Chain with where