module

Lustra::Model::HasColumns

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

Instance methods

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
Source

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.

Source
reset(h : Hash(String, _))

See reset(**t : **T)

Source
reset(h : Hash(Symbol, _))

See reset(**t : **T)

Source
reset

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.

Source
set(h : Hash(String, _))

See set(**t : **T)

Source
set(h : Hash(Symbol, _))

See set(**t : **T)

Source
set

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

model.set(a: 1)
model.a = 1
Source
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}
Source
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 # => {}
Source

Macros

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 Lustra::Model

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

options:

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

  • converter : Class | Module: Use this class to convert the data from the SQL. This class must possess the class methods to_column(::Lustra::SQL::Any) : T and to_db(T) : ::Lustra::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 Lustra about which db column is linked to current field.

  • presence : Bool (default = true): Use this option to let know Lustra 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 Lustra 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 Lustra::Model::JSONDeserialize module.

Source