CRUD Operations
CQL Active Record models provide a comprehensive set of methods for performing CRUD (Create, Read, Update, Delete) operations on your database records. These methods are designed to be intuitive and align with common Active Record patterns.
This guide assumes you have a model defined, for example:
struct User
include CQL::ActiveRecord::Model(Int64)
db_context AcmeDB, :users
property id : Int64?
property name : String
property email : String
property active : Bool = false
# ... other properties and initializers ...
end1. Creating Records
There are several ways to create new records and persist them to the database.
new + save / save!
new + save / save!The most fundamental way is to create a new instance of your model using new and then call save or save! to persist it.
save: Attempts to save the record. Runs validations. If successful, populates theid(if auto-generated) and returnstrue. If validations fail or anotherbefore_savecallback halts the chain, it returnsfalseand does not persist the record. Theerrorscollection on the instance can be inspected.save!: Similar tosave, but raises an exception if the record cannot be saved.Raises
CQL::Errors::RecordInvalidif validations fail.May raise other database-related exceptions or
CQL::Errors::RecordNotSavedfor other save failures.
create / create! (Class Methods)
create / create! (Class Methods)These class methods provide a convenient way to instantiate and save a record in a single step.
Model.create(attributes): Creates a new instance with the given attributes (as a Hash or keyword arguments) and attempts tosaveit. Returns the model instance (which might be invalid and not persisted ifsavefailed) orfalseif abefore_createcallback halted the process.Model.create!(attributes): Similar tocreate, but callssave!internally. It will raise an exception (CQL::Errors::RecordInvalidor other) if the record cannot be created and persisted.
find_or_create_by (Class Method)
find_or_create_by (Class Method)This method attempts to find a record matching the given attributes. If found, it returns that record. If not found, it creates and persists a new record with those attributes (plus any additional ones provided if the find attributes are a subset of create attributes).
It uses
create!internally for the creation part, so it can raise exceptions if the creation fails (e.g., due to validations).
2. Reading Records
CQL provides various methods to retrieve records from the database. Many of these are covered in detail in the Querying Guide.
Summary of Common Finders:
Model.all: Retrieves all records of the model type. ReturnsArray(Model).Model.find?(id : Pk)(orModel.find(id : Pk)): Finds a record by its primary key. ReturnsModel?(the instance ornil).Model.find!(id : Pk): Finds a record by its primary key. ReturnsModelor raisesDB::NoResultsError(or similar if not found).Model.find_by(**attributes): Finds the first record matching the given attributes. ReturnsModel?.Model.find_by!(**attributes): Finds the first record matching attributes. ReturnsModelor raisesDB::NoResultsError.Model.find_all_by(**attributes): Finds all records matching attributes. ReturnsArray(Model).Model.first: Retrieves the first record (ordered by primary key). ReturnsModel?.Model.last: Retrieves the last record (ordered by primary key). ReturnsModel?.Model.count: Returns the total number of records asInt64.Model.query.[condition].count: Counts records matching specific conditions.
For building more complex queries (e.g., with joins, specific selections, grouping), refer to the Querying Guide.
3. Updating Records
To update existing records, you typically load an instance, modify its attributes, and then save it.
Load, Modify, and save / save!
save / save!This is the standard approach:
update / update! (Instance Methods)
update / update! (Instance Methods)These instance methods provide a shortcut to assign attributes and then save the record.
instance.update(attributes): Assigns the given attributes (Hash or keyword arguments) to the instance and then callssave. Returnstrueif successful,falseotherwise.instance.update!(attributes): Assigns attributes and callssave!. Raises an exception if saving fails (e.g.,CQL::Errors::RecordInvalid).
Model.update!(id, attributes) (Class Method)
Model.update!(id, attributes) (Class Method)This class method updates a record identified by its primary key with the given attributes. It typically loads the record, updates attributes, and calls save!. Can raise DB::NoResultsError if the ID is not found, or validation/save exceptions.
Batch Updates (Model.update_by, Model.update_all)
Model.update_by, Model.update_all)These methods allow updating multiple records at once without instantiating each one.
Model.update_by(conditions_hash, updates_hash): Updates all records matchingconditions_hashwith the attributes inupdates_hash.Model.update_all(updates_hash): Updates all records in the table with the attributes inupdates_hash. Use with extreme caution!
These methods typically execute a single SQL UPDATE statement and do not instantiate records, run validations, or trigger callbacks.
4. Deleting Records
Records can be deleted individually or in batches.
delete! (Instance Method)
delete! (Instance Method)Deletes the specific model instance from the database.
Runs
before_destroyandafter_destroycallbacks.Returns
trueif successful. Returnsfalseif abefore_destroycallback halts the operation.
Model.delete!(id : Pk) (Class Method)
Model.delete!(id : Pk) (Class Method)Deletes the record with the specified primary key directly from the database.
This method typically does not run Active Record callbacks (
before_destroy,after_destroy) as it usually issues a direct SQLDELETEcommand.It might raise an exception if the record doesn't exist or if there's a database error.
Batch Deletes (Model.delete_by!, Model.delete_all)
Model.delete_by!, Model.delete_all)These class methods delete multiple records based on conditions or all records from a table.
Model.delete_by!(attributes_hash): Deletes all records matching theattributes_hash.Model.delete_all: Deletes all records from the model's table. Use with extreme caution!
These methods typically execute direct SQL DELETE statements and do not instantiate records or run Active Record callbacks.
Always be careful with batch delete operations, especially delete_all, as they can lead to irreversible data loss if not used correctly.
This guide covers the primary CRUD operations available in CQL Active Record. For more advanced querying, refer to the Querying Guide, and for information on lifecycle events and data integrity, see the guides on Callbacks and Validations.
Last updated
Was this helpful?