abstract class Avram::Model
- Avram::Model
- Reference
- Object
Included Modules
Defined in:
avram/model.crConstant 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
-
#==(other : self)
Returns
true
if this reference is the same as other. - #delete
- #id
- #model_name
-
#reload : self
Reload the model with the latest information from the database
-
#reload(&) : self
Same as
#reload
but allows passing a block to customize the query. - #to_param
Macro Summary
- association(table_name, type, relationship_type, foreign_key = nil, through = nil)
- column(type_declaration, autogenerated = false)
- default_columns
- primary_key(type_declaration)
- register_setup_step(call)
- setup(table_name)
- setup_column_names_method(columns, *args, **named_args)
-
setup_db_mapping(columns, *args, **named_args)
Setup database mapping for the model's columns.
- setup_getters(columns, *args, **named_args)
- setup_initialize(columns, *args, **named_args)
- setup_table_name(table_name, *args, **named_args)
- skip_default_columns
- table(table_name = nil)
- timestamps
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
Instance Method Detail
Returns true
if this reference is the same as other. Invokes same?
.
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
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)
Macro Detail
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.