Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Cql::Relations::Collection
< Reference
< Object
A collection of records for a many to many relationship This class is used to manage the relationship between two tables through a join table (through)
A many-to-many association occurs when multiple records of one model can be associated with multiple records of another model, and vice versa. Typically, it requires a join table (or a junction table) to store the relationships between the records of the two models.
Here’s how a many-to-many association is commonly implemented in CQL using Crystal.
Example
(key : Symbol, id : Pk, target_key : Symbol, 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
Clears all associated records from the parent record and the database
return : [] of T
Example
(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
Create a new record and associate it with the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
raise : Cql::Error
Example
(record : Target)
Delete the associated record from the parent record if it exists
param : record (Target)
return : Bool
Example
(id : Pk)
Delete the associated record from the parent record if it exists
param : id (Pk)
return : Bool
Example
(ids : Array(Int64))
Associates the parent record with the records that match the primary keys provided
param : ids (Array(Pk))
return : Array(Target)
Example
Write documentation for Record module
Example Using the Record module
(attrs : Hash(Symbol, DB::Any))
Set the record's attributes from a hash
@param attrs [Hash(Symbol, DB::Any)] The attributes to set
@return [Nil]
Example Setting the record's attributes
Define instance-level methods for querying and manipulating data Fetch the record's ID or raise an error if it's nil
@return [PrimaryKey] The ID
Example Fetching the record's ID
Delete the record from the database
@return [Nil]
Example Deleting the record
Identity method for the record ID
@return [PrimaryKey] The ID
Example Fetching the record's ID
(id : Pk)
Set the record's ID
@param id [PrimaryKey] The ID
Example Setting the record's ID
Check if the record has been persisted to the database
@return [Bool] True if the record has an ID, false otherwise
Example Checking if the record is persisted
Define instance-level methods for querying and manipulating data Fetch the record's ID or raise an error if it's nil
@return [PrimaryKey] The ID
Example Fetching the record's ID
Define instance-level methods for saving and deleting records Save the record to the database or update it if it already exists
@return [Nil]
Example Saving the record
(fields : Hash(Symbol, DB::Any))
Delete the record from the database if it exists
@return [Nil]
Example Deleting the record
Update the record with the given record object
Example Updating the record
Update the record with the given fields
@param fields [Hash(Symbol, DB::Any)] The fields to update
@return [Nil]
Example Updating the record
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
(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
(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
Create a new record and associate it with the parent record
return : Array(Target)
Example
Clears all associated records from the parent record and the database
return : [] of T
Example
(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
Create a new record and associate it with the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
raise : Cql::Error
Example
(record : Target)
Delete the associated record from the parent record if it exists
param : record (Target)
return : Bool
Example
(id : Pk)
Delete the associated record from the parent record if it exists
param : id (Pk)
return : Bool
Example
Check if the association is empty or not
return : Bool
Example
Check if the association exists or not based on the attributes provided
param : attributes (Hash(Symbol, String | Int64))
return : Bool
Example
Find associated records based on the attributes provided for the parent record
param : attributes (Hash(Symbol, String | Int64))
return : Array(Target)
Example
Returns a list if primary keys for the associated records
return : Array(Pk)
Example
(ids : Array(Pk))
Associates the parent record with the records that match the primary keys provided
param : ids (Array(Pk))
return : Array(Target)
Example
Reload the association records from the database and return them
return : Array(Target)
Example
Returns the number of associated records for the parent record
return : Int64
Example
(call)
Define the has_many association module that will be included in the model to define a one-to-many relationship between two tables in the database 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 : name (Symbol) - The name of the association
param : type (Cql::Model) - The target model
param : foreign_key (Symbol) - The foreign key column in the target table
return : Nil
Example
(name, type, foreign_key, cascade = false)
(name, type, join_through, cascade = false)
Defines a many-to-many relationship between two models. This method will define a getter method that returns a ManyToMany::Collection. The collection can be used to add and remove records from the join table.
param : name (Symbol) - The name of the association
param : type (Cql::Model) - The target model
param : join_through (Cql::Model) - The join table model
param : cascade (Bool) - Delete associated records
Example