module

Avram::PrimaryKeyMethods

Instance methods

==(other : self)
Source
delete
Source
primary_key_name
Source
reload

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 not 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
Source
reload

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)
Source
reload?

Reload the model with the latest information from the database, if it still exists.

This method will return a new model instance with the latest data from the database, or Nil if the record has been deleted in the meantime.

Example:

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

# ... record is deleted

user.reload?
# => Nil
Source
reload?

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)

# ... record is deleted

user = user.reload?(&.preload_comments(CommentQuery.new.preload_article))
# => Nil
Source
to_param

For integration with Lucky This allows an Avram::Model to be passed into a Lucky::Action to create a url/path

Source