class
CQL::ActiveRecord::Relations::Collection(Target, Pk)
- CQL::ActiveRecord::Relations::Collection(Target, Pk)
- Reference
- Object
Overview
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
class User
include CQL::ActiveRecord::Model(Int64)
property id : Int64
property name : String
has_many :posts, Post, foreign_key: :user_id
end
Included Modules
- Enumerable(Target)
Direct Known Subclasses
Defined in:
active_record/relations/collection.crConstructors
-
.new(key : Symbol, id : Pk, cascade : Bool = false, query : CQL::Query = (CQL::Query.new(Target.schema)).from(Target.table), auto_load : Bool = true)
Initialize the collection class for a one-to-many relationship - param : key (Symbol) - The foreign key in the target table (e.g., :user_id) - param : id (Pk) - The id value for the parent record - param : cascade (Bool) - Whether to delete associated records when parent is deleted - param : query (CQL::Query) - Base query object for the relationship - param : auto_load (Bool) - Whether to automatically load associated records - return : Collection(Target, Pk)
Macro Summary
Instance Method Summary
-
#<<(record : Target)
Adds a record to the collection and saves it to the database - param : record (Target) - return : Array(Target)
-
#all : Array(Target)
Returns all associated records - return : Array(Target)
-
#build(**attributes)
Creates a new, unsaved record with the parent association set - param : attributes (NamedTuple | Hash(Symbol, DB::Any)) - return : Target
-
#clear
Clears all associated records from the parent record - return : Int64 - Number of records deleted
-
#create(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
-
#create(**attributes)
Creates a new record with the given attributes and saves it - param : attributes (NamedTuple | Hash(Symbol, DB::Any)) - return : Target - raise : CQL::Error
-
#delete(record : Target)
Delete the associated record from the parent record if it exists - param : record (Target) - return : Bool
-
#delete(id : Pk)
Delete the associated record from the parent record if it exists - param : id (Pk) - return : Bool
-
#each(&block : Target -> )
Implements the each method for the Enumerable module - param : block (Block(Target)) - return : Nil
-
#empty?
Checks if the collection is empty - return : Bool
-
#exists?(**attributes)
Checks if any records exist with the given attributes - param : attributes (NamedTuple) - return : Bool
-
#find(**attributes)
Find associated records matching the given attributes - param : attributes (NamedTuple | Hash(Symbol, DB::Any)) - return : Array(Target)
-
#find_by(**attributes)
Finds a single record by attributes - param : attributes (NamedTuple | Hash(Symbol, DB::Any)) - return : Target?
-
#first
Returns the first record in the collection - return : Target?
-
#ids : Array(Pk)
Returns a list of primary keys for the associated records - return : Array(Pk)
-
#ids=(ids : Array(Pk))
Associates the parent record with the records that match the primary keys provided - param : ids (Array(Pk)) - return : Array(Target)
-
#reload
Reloads the association records from the database - return : Array(Target)
-
#size
Returns the number of associated records - return : Int32
-
#where(**conditions)
Returns a new query for chaining where conditions - param : conditions (NamedTuple | Hash(Symbol, DB::Any)) - return : CQL::Query
Constructor Detail
Initialize the collection class for a one-to-many relationship
- param : key (Symbol) - The foreign key in the target table (e.g., :user_id)
- param : id (Pk) - The id value for the parent record
- param : cascade (Bool) - Whether to delete associated records when parent is deleted
- param : query (CQL::Query) - Base query object for the relationship
- param : auto_load (Bool) - Whether to automatically load associated records
- return : Collection(Target, Pk)
Example
Collection(Post, Int64).new(
:user_id,
1,
cascade: true,
query: CQL::Query.new(Post.schema).from(Post.table)
)
Macro Detail
Instance Method Detail
Adds a record to the collection and saves it to the database
- param : record (Target)
- return : Array(Target)
Example
user.posts << Post.new(title: "Hello World")
=> [#<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">]
Returns all associated records
- return : Array(Target)
Example
user.posts.all
=> [#<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">]
Creates a new, unsaved record with the parent association set
- param : attributes (NamedTuple | Hash(Symbol, DB::Any))
- return : Target
Example
post = user.posts.build(title: "New Post")
post.save! # => true
Clears all associated records from the parent record
- return : Int64 - Number of records deleted
Example
user.posts.create(title: "Hello World")
user.posts.reload
user.posts.size => 1
user.posts.clear
user.posts.reload
user.posts.size => 0
Create a new record and associate it with the parent record
- param : attributes (Hash(Symbol, String | Int64))
- return : Array(Target)
- raise : CQL::Error
Example
movie.actors.create!(name: "Hugo Weaving")
movie.actors.reload
movie.actors.all
=> [#<Actor:0x00007f8b3b1b3f00 @id=1, @name="Hugo Weaving">]
Creates a new record with the given attributes and saves it
- param : attributes (NamedTuple | Hash(Symbol, DB::Any))
- return : Target
- raise : CQL::Error
Example
user.posts.create(title: "Hello World")
=> #<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">
Delete the associated record from the parent record if it exists
- param : record (Target)
- return : Bool
Example
movie.actors.create(name: "Carrie-Anne Moss")
movie.actors.reload
movie.actors.all => 1
movie.actors.delete(Actor.find(1))
movie.actors.reload
movie.actors.all
=> [] of Actor
Delete the associated record from the parent record if it exists
- param : id (Pk)
- return : Bool
Example
movie.actors.create(name: "Carrie-Anne Moss")
movie.actors.reload
movie.actors.all => 1
movie.actors.delete(1)
movie.actors.reload
movie.actors.all => []
Implements the each method for the Enumerable module
- param : block (Block(Target))
- return : Nil
Checks if any records exist with the given attributes
- param : attributes (NamedTuple)
- return : Bool
Example
user.posts.exists?(title: "Hello World")
=> true
Find associated records matching the given attributes
- param : attributes (NamedTuple | Hash(Symbol, DB::Any))
- return : Array(Target)
Example
user.posts.find(title: "Hello World")
=> [#<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">]
Finds a single record by attributes
- param : attributes (NamedTuple | Hash(Symbol, DB::Any))
- return : Target?
Example
user.posts.find_by(title: "Hello World")
=> #<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">
Returns the first record in the collection
- return : Target?
Example
user.posts.first
=> #<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">
Returns a list of primary keys for the associated records
- return : Array(Pk)
Example
user.posts.ids
=> [1, 2, 3]
Associates the parent record with the records that match the primary keys provided
- param : ids (Array(Pk))
- return : Array(Target)
Example
movie.actors.ids = [1, 2, 3]
movie.actors.reload
movie.actors.all => [
#<Actor:0x00007f8b3b1b3f00 @id=1, @name="Carrie-Anne Moss">,
#<Actor:0x00007f8b3b1b3f00 @id=2, @name="Hugo Weaving">,
#<Actor:0x00007f8b3b1b3f00 @id=3, @name="Laurence Fishburne">]
Reloads the association records from the database
- return : Array(Target)
Example
user.posts.reload
=> [#<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">]
Returns a new query for chaining where conditions
- param : conditions (NamedTuple | Hash(Symbol, DB::Any))
- return : CQL::Query
Example
user.posts.where(title: "Hello").where(published: true).all
=> [#<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello", @published=true>]