Record Models
The Cql::Record
module in the CQL toolkit is a crucial part of the Object-Relational Mapping (ORM) system in Crystal. It allows you to define models that map to tables in your database and provides a wide array of functionalities for querying, inserting, updating, and deleting records. In this guide, we'll explore how the Cql::Record
module works and how to use it effectively.
Example: Building a Simple Blog System
Let's combine everything we've learned to build a simple blog system where posts can have many comments.
Defining the Schema:
Defining the Models:
Using the Models:
Creating a Post:
Adding Comments to the Post:
Fetching Comments for a Post:
Conclusion
The Cql::Record
module provides powerful tools for working with database records in a Crystal application. It simplifies the process of defining models, querying records, and managing associations. By leveraging the capabilities of CQL's Active Record-style ORM, you can build complex applications with ease.
With Cql::Record
, you have access to:
Easy schema and model definition.
A rich set of query and manipulation methods.
Powerful association handling (
belongs_to
,has_many
,
Last updated