abstract class Avram::Model

Included Modules

Defined in:

avram/model.cr

Constant Summary

MACRO_CHECKS = {setup_complete: false}

This setting is used to show better errors

SETUP_STEPS = [Avram::Model.setup_table_name, Avram::Model.setup_initialize, Avram::Model.setup_db_mapping, Avram::Model.setup_getters, Avram::Model.setup_column_names_method, Avram::BaseQueryTemplate.setup, Avram::SaveOperationTemplate.setup, Avram::SchemaEnforcer.setup] of Nil

Class Method Summary

Instance Method Summary

Macro Summary

Class methods inherited from module Avram::SchemaEnforcer

ensure_correct_column_mappings! ensure_correct_column_mappings!

Instance methods inherited from class Object

blank_for_validates_required? : Bool blank_for_validates_required?

Class Method Detail

def self.table_name #

[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


[View source]
def delete #

[View source]
abstract def id #

[View source]
def model_name #

[View source]
def reload : self #

Reload the model with the latest information from the database

This method will return a new model instance with the latest data from the database. Note that this does note change the original instance, so you may need to assign the result to a variable or work directly with the return value.

Example:

user = SaveUser.create!(name: "Original")
SaveUser.update!(user, name: "Updated")

# Will be "Original"
user.name
# Will return "Updated"
user.reload.name # Will be "Updated"
Will still be "Original" since the 'user' is the same model instance.
user.name

Instead re-assign the variable. Now 'name' will return "Updated" since
'user' references the reloaded model.
user = user.reload
user.name

[View source]
def reload(&) : self #

Same as #reload but allows passing a block to customize the query.

This is almost always used to preload additional relationships.

Example:

user = SaveUser.create(params)

# We want to display the list of articles the user has commented on, so let's #
# preload them to avoid N+1 performance issues
user = user.reload(&.preload_comments(CommentQuery.new.preload_article))

# Now we can safely get all the comment authors
user.comments.map(&.article)

Note that the yielded query is the BaseQuery so it will not have any methods defined on your customized query. This is usually fine since typically reload only uses preloads.

If you do need to do something more custom you can manually reload:

user = SaveUser.create!(name: "Helen")
UserQuery.new.some_custom_preload_method.find(user.id)

[View source]
def to_param #

[View source]

Macro Detail

macro association(table_name, type, relationship_type, foreign_key = nil, through = nil) #

[View source]
macro column(type_declaration, autogenerated = false) #

[View source]
macro default_columns #

[View source]
macro primary_key(type_declaration) #

[View source]
macro register_setup_step(call) #

[View source]
macro setup(table_name) #

[View source]
macro setup_column_names_method(columns, *args, **named_args) #

[View source]
macro setup_db_mapping(columns, *args, **named_args) #

Setup database mapping for the model's columns.

NOTE Avram::Migrator saves Float columns as numeric which need to be converted from PG::Numeric back to Float64 using a convertor class.


[View source]
macro setup_getters(columns, *args, **named_args) #

[View source]
macro setup_initialize(columns, *args, **named_args) #

[View source]
macro setup_table_name(table_name, *args, **named_args) #

[View source]
macro skip_default_columns #

[View source]
macro table(table_name = nil) #

[View source]
macro timestamps #

[View source]