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.crInstance Method Summary
-
#[](x) : Clear::SQL::Any
Access to direct SQL attributes given by the request used to build the model.
-
#[]?(x) : Clear::SQL::Any
Access to direct SQL attributes given by the request and used to build the model or Nil if not found.
- #reset(h : Hash(String, _))
- #reset(h : Hash(Symbol, _))
-
#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. -
#set(h : Hash(String, _))
See
#set(**t : **T)
-
#set(h : Hash(Symbol, _))
See
#set(**t : **T)
-
#set(**t : **T) forall T
Set one or multiple columns to a specific value This two are equivalents:
-
#to_h(full = false)
Returns the model columns as Hash.
-
#update_h
Returns the current hash of the modified values:
Macro Summary
-
column(name, primary = false, converter = nil, column_name = nil, presence = true, mass_assign = true)
Bind a column to the model.
Instance Method Detail
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
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.
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.
Set one or multiple columns to a specific value This two are equivalents:
model.set(a: 1)
model.a = 1
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}
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 # => {}
Macro Detail
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 methodsto_column(::Clear::SQL::Any) : T
andto_db(T) : ::Clear::SQL::Any
withT
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 parametercolumn_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 theClear::Model::JSONDeserialize
module.