module CQL::ActiveRecord::Relations::HasMany

Overview

Enhanced has_many association module with improved type safety, error handling, and performance optimizations.

Provides methods to define and manage one-to-many relationships where the foreign key is stored in the associated model. Supports cascading deletes, lazy loading, and proper dependency management.

Included Modules

Defined in:

active_record/relations/has_many.cr

Macro Summary

Macros inherited from module CQL::ActiveRecord::Relations::BaseRelation

build_query(model_class) build_query, destroy_dependent(record, association) destroy_dependent, ensure_saved(record) ensure_saved, safe_db_operation(&block) safe_db_operation, safe_foreign_key(record, key, pk_type) safe_foreign_key, safe_id(record, pk_type) safe_id, underscore_name(class_name) underscore_name

Macro Detail

macro has_many(name, type, foreign_key = nil, dependent = nil, inverse_of = nil, scope = nil) #

Define the has_many association with enhanced features

  • param : name (Symbol) - The name of the association
  • param : type (Class) - The target model class
  • param : foreign_key (Symbol) - The foreign key column in the target table (optional)
  • param : dependent (Symbol) - What delete! does with the associated records (:destroy, :delete_all, :nullify, :restrict_with_error); without it, deleting the parent leaves them alone and clear nullifies
  • param : inverse_of (Symbol) - The inverse association name (optional)
  • param : scope (Proc) - Additional scope for the association (optional)

Example

class Article
  include CQL::ActiveRecord::Model(Int64)
  db_context AcmeDB, :articles

  property author_id : Int64?
end

class Author
  include CQL::ActiveRecord::Model(Int64)
  db_context AcmeDB, :authors

  has_many :articles, Article, foreign_key: :author_id, dependent: :destroy
end

[View source]