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:
The definition of this model is straight forward:
Cut step by step, this is what happens:
First, we include all the magic of Clear in our class:
Second, we define name
column as String. Clear will map automatically the column to the model attribute.
Third, we define description
. We defined description
as NULLABLE
in our database. To reflect this choice, we add Nilable
?
operator to our column.
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.
You may now use your model :
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
:
Next article is covering deeply the column definition and its subtleties.
Last updated