module CQL::ActiveRecord::Relations::HasOne

Overview

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

Provides methods to define and manage one-to-one relationships where the foreign key is stored in the associated model.

Included Modules

Defined in:

active_record/relations/has_one.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_one(name, kind, foreign_key = nil, dependent = nil, cache = true) #

Define the has_one association with enhanced features

  • param : name (Symbol) - Association name
  • param : kind (Class) - Associated model class
  • param : foreign_key (Symbol) - Foreign key in the associated model (optional)
  • param : dependent (Symbol) - What to do with the associated record (:destroy, :delete, :nullify): when it is replaced or deleted through the association (default :destroy), and, only when declared, when the parent is deleted
  • param : cache (Bool) - Whether to cache the associated record

Example

class Profile
  include CQL::ActiveRecord::Model(Int64)
  db_context AcmeDB, :profiles

  property account_id : Int64?
end

class Account
  include CQL::ActiveRecord::Model(Int64)
  db_context AcmeDB, :accounts

  has_one :profile, Profile, foreign_key: :account_id, dependent: :destroy
end

[View source]