module Clear::Model::HasColumns

Overview

This module declare all the methods and macro related to columns in Clear::Model

Direct including types

Defined in:

clear/model/modules/has_columns.cr

Instance Method Summary

Macro Summary

Instance Method Detail

def [](x) : Clear::SQL::Any #

Access to direct SQL attributes given by the request used to build the model. Access is read only and updating the model columns will not apply change to theses columns.

model = Model.query.select("MIN(id) as min_id").first(fetch_columns: true)
id = model["min_id"].to_i32

[View source]
def []?(x) : Clear::SQL::Any #

Access to direct SQL attributes given by the request and used to build the model or Nil if not found.

Access is read only and updating the model columns will not apply change to theses columns. You must set fetch_column: true in your model to access the attributes.


[View source]
def reset(h : Hash(String, _)) #

[View source]
def reset(h : Hash(Symbol, _)) #

[View source]
def reset(**t : **T) forall T #

Reset one or multiple columns; Reseting set the current value of the column to the given value, while the changed? flag remains false. If you call save on a persisted model, the reset columns won't be commited in the UPDATE query.


[View source]
def set(h : Hash(String, _)) #

[View source]
def set(h : Hash(Symbol, _)) #

[View source]
def set(**t : **T) forall T #

Set one or multiple columns to a specific value This two are equivalents:

model.set(a: 1)
model.a = 1

[View source]
def to_h(full = false) #

Returns the model columns as Hash. Calling #to_h will returns only the defined columns, while settings the optional parameter full to true will return all the column and fill the undefined columns by nil values. Example:

# Assuming our model has a primary key, a first name and last name and two timestamp columns:
model = Model.query.select("first_name, last_name").first!
model.to_h             # => { "first_name" => "Johnny", "last_name" => "Walker" }
model.to_h(full: true) # => {"id" => nil, "first_name" => "Johnny", "last_name" => "Walker", "created_at" => nil, "updated_at" => nil}

[View source]
def update_h #

Returns the current hash of the modified values:

model = Model.query.first!
model.update_h # => {}
model.first_name = "hello"
model.update_h # => { "first_name" => "hello" }
model.save!
model.update_h # => {}

[View source]

Macro Detail

macro column(name, primary = false, converter = nil, column_name = nil, presence = true, mass_assign = true) #

Bind a column to the model.

Simple example:

class MyModel
  include Clear::Model

  column some_id : Int32, primary: true
  column nullable_column : String?
end

options:

  • primary : Bool: Let Clear ORM know which column is the primary key. Currently compound primary key are not compatible with Clear ORM.

  • converter : Class | Module: Use this class to convert the data from the SQL. This class must possess the class methods to_column(::Clear::SQL::Any) : T and to_db(T) : ::Clear::SQL::Any with T the type of the column.

  • column_name : String: If the name of the column in the model doesn't fit the name of the column in the SQL, you can use the parameter column_name to tell Clear about which db column is linked to current field.

  • presence : Bool (default = true): Use this option to let know Clear that your column is not nullable but with default value generated by the database on insert (e.g. serial) During validation before saving, the presence will not be checked on this field and Clear will try to insert without the field value.

  • mass_assign : Bool (default = true): Use this option to turn on/ off mass assignment when instantiating or updating a new model from json through .from_json methods from the Clear::Model::JSONDeserialize module.


[View source]