class CQL::ActiveRecord::Relations::Collection(Target, Pk)

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.

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

Direct Known Subclasses

Defined in:

active_record/relations/collection.cr

Constructors

Macro Summary

Instance Method Summary

Constructor Detail

def self.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)

Example

Collection(Post, Int64).new(
  :user_id,
  1,
  cascade: true,
  query: CQL::Query.new(Post.schema).from(Post.table)
)

[View source]

Macro Detail

macro method_missing(call) #

[View source]

Instance Method Detail

def <<(record : Target) #

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">]

[View source]
def all : Array(Target) #

Returns all associated records

  • return : Array(Target)

Example

user.posts.all
=> [#<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">]

[View source]
def build(**attributes) #

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

[View source]
def clear #

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

[View source]
def 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

Example

movie.actors.create!(name: "Hugo Weaving")
movie.actors.reload
movie.actors.all
=> [#<Actor:0x00007f8b3b1b3f00 @id=1, @name="Hugo Weaving">]

[View source]
def 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

Example

user.posts.create(title: "Hello World")
=> #<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">

[View source]
def delete(record : Target) #

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

[View source]
def delete(id : Pk) #

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 => []

[View source]
def each(&block : Target -> ) #

Implements the each method for the Enumerable module

  • param : block (Block(Target))
  • return : Nil

[View source]
def empty? #

Checks if the collection is empty

  • return : Bool

Example

user.posts.empty?
=> true

[View source]
def exists?(**attributes) #

Checks if any records exist with the given attributes

  • param : attributes (NamedTuple)
  • return : Bool

Example

user.posts.exists?(title: "Hello World")
=> true

[View source]
def find(**attributes) #

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">]

[View source]
def find_by(**attributes) #

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">

[View source]
def first #

Returns the first record in the collection

  • return : Target?

Example

user.posts.first
=> #<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">

[View source]
def ids : Array(Pk) #

Returns a list of primary keys for the associated records

  • return : Array(Pk)

Example

user.posts.ids
=> [1, 2, 3]

[View source]
def ids=(ids : Array(Pk)) #

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">]

[View source]
def reload #

Reloads the association records from the database

  • return : Array(Target)

Example

user.posts.reload
=> [#<Post:0x00007f8b3b1b3f00 @id=1, @title="Hello World">]

[View source]
def size #

Returns the number of associated records

  • return : Int32

Example

user.posts.size
=> 1

[View source]
def where(**conditions) #

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>]

[View source]