class CQL::Relations::Collection(Target, Pk)
Reference
< Object
A collection of records for a one to many relationship This class is used to manage the relationship between two tables through a foreign key column in the target table and provide methods to manage the association between the two tables and query records in the associated table based on the foreign key value of the parent record.
param : Target (CQL::Model) - The target model
param : Pk (Int64) - The primary key type
return : Nil
Example
Constructors
def new(key : Symbol, id : Pk, cascade : Bool = false, query : CQL::Query = (CQL::Query.new(Target.schema)).from(Target.table))
(key : Symbol, id : Pk, cascade : Bool = false, query : CQL::Query = (CQL::Query.new(Target.schema)).from(Target.table))
Initialize the many-to-many association collection class
param : key (Symbol) - The key for the parent record
param : id (Pk) - The id value for the parent record
param : target_key (Symbol) - The key for the associated record
param : cascade (Bool) - Delete associated records
param : query (CQL::Query) - Query object
return : ManyCollection
Example
Instance Methods
def <<(record : Target)
(record : Target)
Create a new record and associate it with the parent record if it doesn't exist
param : record (Target)
return : Array(Target)
Example
def all
Create a new record and associate it with the parent record
return : Array(Target)
Example
def clear
Clears all associated records from the parent record and the database
return : [] of T
Example
def create(record : Target)
(record : Target)
Create a new record and associate it with the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
raise : CQL::Error
Example
def create
Create a new record and associate it with the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
raise : CQL::Error
Example
def delete(record : Target)
(record : Target)
Delete the associated record from the parent record if it exists
param : record (Target)
return : Bool
Example
def delete(id : Pk)
(id : Pk)
Delete the associated record from the parent record if it exists
param : id (Pk)
return : Bool
Example
def empty?
Check if the association is empty or not
return : Bool
Example
def exists?
Check if the association exists or not based on the attributes provided
param : attributes (Hash(Symbol, String | Int64))
return : Bool
Example
def find
Find associated records based on the attributes provided for the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
Example
def ids
Returns a list if primary keys for the associated records
return : Array(Pk)
Example
def ids=(ids : Array(Pk))
(ids : Array(Pk))
Associates the parent record with the records that match the primary keys provided
param : ids (Array(Pk))
return : Array(Target)
Example
def reload
Reload the association records from the database and return them
return : Array(Target)
Example
def size
Returns the number of associated records for the parent record
return : Int64
Example
Macros
macro method_missing(call)
(call)
Last updated