Clear ORM
  • Welcome to Clear
  • Introduction
    • Setup
  • Model
    • Defining your model
      • Describing your columns
      • Primary Keys
      • Converters
    • Associations
      • belongs_to
      • has_many
      • has_many through
      • has_one
    • Lifecycle
      • Persistence
      • Validations
      • Triggers
    • Batchs operations
      • Bulk update
      • Bulk insert & delete
    • Transactions & Save Points
      • Transaction & Savepoints
      • Connection pool
    • Locks
  • Querying
    • The collection object
      • Filter the query
        • Filter the query – The Expression Engine
        • Find, First, Last, Offset, Limit
        • Aggregation
        • Ordering & Group by
      • Fetching the query
        • Each and Fetch
        • Cursored fetching
        • Model extra attributes
      • Joins
      • Eager Loading
      • Window and CTE
      • Scopes
    • Writing low-level SQL
      • Select Clause
      • Insert Clause
      • Delete Clause
  • Migrations
    • Manage migrations
    • Call migration script
    • Migration CLI
  • Additional and advanced features
    • JSONB
    • Symbol vs String
    • Enums
    • BCrypt
    • Full Text Search
    • Handling multi-connection
  • Other resources
    • API Documentation
    • Inline documentation
    • Github repository
    • Credits
    • Benchmark
Powered by GitBook
On this page
  1. Model

Defining your model

Model definition in Clear is done by inclusion of the Clear::Model module in your class. Assuming we have this table in PostgreSQL:

CREATE TABLE articles (
   id serial NOT NULL PRIMARY KEY,
   name text NOT NULL,
   description text
);

The definition of this model is straight forward:

article.cr
class Article
  include Clear::Model

  column name : String
  column description : String?

  column id : Int32, primary: true, presence: false
end

Cut step by step, this is what happens:

First, we include all the magic of Clear in our class:

include Clear::Model

Second, we define name column as String. Clear will map automatically the column to the model attribute.

column name : String

Third, we define description . We defined descriptionas NULLABLE in our database. To reflect this choice, we add Nilable ? operator to our column.

column description : String?

Finally, we define id as our primary key for this model. While being declared as NOT NULL, the column is defined with a default value in PostgreSQL. Therefore, we tell Clear to not check value presence on save/validate by adding presence: false to the column definition.

column id : Int32, primary: true, presence: false

You may now use your model :

a = Article.new({name: "A superb article!" })
a.description = "This is a master piece!"
a.save!

puts "Article has been properly saved as id=#{a.id}"

By default, Clear will inflect the model name and use plural lower case version of the model name as table name (here articles).

You may want to override this behavior, by redefining self.table :

class Model::Customer
  include Clear::Model

  self.table = "clients" #< Different from infered "model_customers" table.
  # ...
end

Next article is covering deeply the column definition and its subtleties.

PreviousSetupNextDescribing your columns

Last updated 5 years ago