Post
Inherits Lustra::Model < Lustra::Model::FullTextSearchable < Lustra::Model::JSONDeserialize < Lustra::Model::Initializer < Lustra::Model::HasFactory < Lustra::Model::Introspection < Lustra::Model::ClassMethods < Lustra::Model::HasScope < Lustra::Model::HasRelations < Lustra::Model::HasValidation < Lustra::Validation::Helper < Lustra::Model::HasSaving < Lustra::Model::HasSerialPkey < Lustra::Model::HasTimestamps < Lustra::Model::HasColumns < Lustra::Model::HasHooks < Lustra::Model::Connection < Lustra::ErrorMessages < Reference < Object
Constants
Constructors
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Build and new model and save it. Returns the model.
The model may not be saved due to validation failure;
check the returned model errors? and persisted? flags.
Build and new model and save it. Returns the model.
The model may not be saved due to validation failure;
check the returned model errors? and persisted? flags.
Build and new model and save it. Returns the model.
The model may not be saved due to validation failure;
check the returned model errors? and persisted? flags.
Build and new model and save it. Returns the model.
The model may not be saved due to validation failure;
check the returned model errors? and persisted? flags.
Build and new model and save it. Returns the model.
Returns the newly inserted model Raises an exception if validation failed during the saving process.
Build and new model and save it. Returns the model.
Returns the newly inserted model Raises an exception if validation failed during the saving process.
Build and new model and save it. Returns the model.
Returns the newly inserted model Raises an exception if validation failed during the saving process.
Build and new model and save it. Returns the model.
Returns the newly inserted model Raises an exception if validation failed during the saving process.
Class methods
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Build a new empty model and fill the columns using the NamedTuple in argument.
Returns the new model
Define on which connection the model is living. Useful in case of models living in different databases.
Is set to "default" by default.
See Lustra::SQL#init(URI, *opts) for more information about multi-connections.
Example:
Lustra::SQL.init("postgres://postgres@localhost/database_1")
Lustra::SQL.init("secondary", "postgres://postgres@localhost/database_2")
class ModelA
include Lustra::Model
# Performs all the queries on `database_1`
# self.connection = "default"
column id : Int32, primary: true, presence: false
column title : String
end
class ModelB
include Lustra::Model
# Performs all the queries on `database_2`
self.connection = "secondary"
column id : Int32, primary: true, presence: false
end
Define on which connection the model is living. Useful in case of models living in different databases.
Is set to "default" by default.
See Lustra::SQL#init(URI, *opts) for more information about multi-connections.
Example:
Lustra::SQL.init("postgres://postgres@localhost/database_1")
Lustra::SQL.init("secondary", "postgres://postgres@localhost/database_2")
class ModelA
include Lustra::Model
# Performs all the queries on `database_1`
# self.connection = "default"
column id : Int32, primary: true, presence: false
column title : String
end
class ModelB
include Lustra::Model
# Performs all the queries on `database_2`
self.connection = "secondary"
column id : Int32, primary: true, presence: false
end
Create a new model from json and save it. Returns the model.
The model may not be saved due to validation failure;
check the returned model errors? and persisted? flags.
Trusted flag set to true will allow mass assignment without protection, FALSE by default
Create a new model from json and save it. Returns the model.
Returns the newly inserted model Raises an exception if validation failed during the saving process. Trusted flag set to true will allow mass assignment without protection, FALSE by default
Find multiple models by an array of primary keys. Returns an array of models (may be empty if none found).
users = User.find([1, 2, 3])
users.size # => 0..3 depending on how many were found
Find multiple models by an array of primary keys. Raises error if ANY of the IDs are not found.
users = User.find!([1, 2, 3]) # Raises if any ID is not found
Find a model by column values. Returns nil if not found.
This is an alias for query.find(**tuple) with better naming.
user = User.find_by(email: "test@example.com")
user = User.find_by(first_name: "John", last_name: "Doe")
Find a model by column values. Returns nil if not found.
This is an alias for query.find(**tuple) with better naming.
user = User.find_by(email: "test@example.com")
user = User.find_by(first_name: "John", last_name: "Doe")
Find a model by column values. Raises error if not found.
This is an alias for query.find!(**tuple) with better naming.
user = User.find_by!(email: "test@example.com")
Find a model by column values. Raises error if not found.
This is an alias for query.find!(**tuple) with better naming.
user = User.find_by!(email: "test@example.com")
Create a new empty model and fill the columns from json. Returns the new model
Trusted flag set to true will allow mass assignment without protection, FALSE by default
returns the fully qualified and escaped name for this table. add schema if schema is different from 'public' (default schema)
ex: "schema"."table"
Import a bulk of models in one SQL insert query. Each model must be non-persisted.
on_conflict callback can be optionnaly turned on
to manage constraints of the database.
Note: Old models are not modified. This method return a copy of the models as saved in the database.
Example:
users = [User.new(id: 1), User.new(id: 2), User.new(id: 3)]
users = User.import(users)
Return an empty, chainable collection (Rails-like .none).
Useful for conditional branches where no records should be returned
while keeping query chaining intact.
User.none.where { active == true }.count # => 0
Return a new query SELECT * FROM [my_model_table]. Can be refined after that.
Automatically applies default_scope if defined.
Register a counter cache for this model using model class
Reset counter cache columns to their correct values. This is useful when counter caches become out of sync due to direct SQL operations.
Example:
User.reset_counters(user.id, Post)
User.reset_counters(user.id, Post, Comment)
Define the current schema used in PostgreSQL. The value is nil by default, which lead to non-specified
schema during the querying, and usage of "public" by PostgreSQL.
This property can be redefined on initialization. Example:
class MyModel
include Lustra::Model
self.schema = "my_schema"
end
MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"
Define the current schema used in PostgreSQL. The value is nil by default, which lead to non-specified
schema during the querying, and usage of "public" by PostgreSQL.
This property can be redefined on initialization. Example:
class MyModel
include Lustra::Model
self.schema = "my_schema"
end
MyModel.query.to_sql # SELECT * FROM "my_schema"."my_models"
Return the table name setup for this model. By convention, the class name is by default equals to the pluralized underscored string form of the model name. Example:
MyModel => "my_models"
Person => "people"
Project::Info => "project_infos"
The property can be updated at initialization to a custom table name:
class MyModel
include Lustra::Model
self.table = "another_table_name"
end
MyModel.query.to_sql # SELECT * FROM "another_table_name"
Return the table name setup for this model. By convention, the class name is by default equals to the pluralized underscored string form of the model name. Example:
MyModel => "my_models"
Person => "people"
Project::Info => "project_infos"
The property can be updated at initialization to a custom table name:
class MyModel
include Lustra::Model
self.table = "another_table_name"
end
MyModel.query.to_sql # SELECT * FROM "another_table_name"
Instance methods
Handle append operation for through associations (called by autosave system)
Add a built association to track for autosave
Attributes, used when fetch_columns is true
The method author is a belongs_to relation to Author
Returns the value of author_id column or throw an exception if the column is not defined.
Setter for author_id column.
Returns the column object used to manage author_id field
See Lustra::Model::Column
Track built associations for autosave functionality
Track built associations for autosave functionality
Returns an array of names of all changed attributes.
user.email = "new@test.com"
user.first_name = "John"
user.changed # => ["email", "first_name"]
Return true if the model is dirty (e.g. one or more fields
have been changed.). Return false otherwise.
Returns a hash of all changed attributes with their [old_value, new_value].
user.email = "new@test.com"
user.first_name = "John"
user.changes # => {"email" => ["old@test.com", "new@test.com"], "first_name" => [nil, "John"]}
Reset the changed? flag on all columns
The model behave like its not dirty anymore and call to save would apply no changes.
Returns self
Returns the value of content column or throw an exception if the column is not defined.
Setter for content column.
Returns the column object used to manage content field
See Lustra::Model::Column
Returns the value of created_at column or throw an exception if the column is not defined.
Setter for created_at column.
Returns the column object used to manage created_at field
See Lustra::Model::Column
Returns the value of id column or throw an exception if the column is not defined.
Setter for id column.
Returns the column object used to manage id field
See Lustra::Model::Column
Returns the value of kind column or throw an exception if the column is not defined.
Setter for kind column.
Returns the column object used to manage kind field
See Lustra::Model::Column
Set the columns from hash
Set the model fields from hash
reset flavors
Reset counter cache columns
Example:
user = User.find(1)
user.reset_counters(Post)
user.reset_counters(Post, Comment)
Set the columns from hash
Set the model fields from hash
Set one or multiple columns to a specific value This two are equivalents:
model.set(a: 1)
model.a = 1
Set the fields from json passed as argument Trusted flag set to true will allow mass assignment without protection, FALSE by default
Returns the value of title column or throw an exception if the column is not defined.
Setter for title column.
Returns the column object used to manage title field
See Lustra::Model::Column
Return a hash version of the columns of this model.
Updates timestamp columns without triggering validations or callbacks.
user.touch # Updates updated_at
user.touch(2.days.ago) # Updates updated_at to specific time
Updates the specified column and updated_at without triggering validations or callbacks.
user.touch(:last_login_at) # Updates last_login_at and updated_at
user.touch(:last_seen_at, 1.hour.ago)
Updates multiple timestamp columns without triggering validations or callbacks.
user.touch([:last_login_at, :last_seen_at])
user.touch([:last_login_at, :last_seen_at], 3.days.ago)
Updates multiple timestamp columns at once.
user.touch(:last_login_at, :last_seen_at)
user.touch(:last_login_at, :last_seen_at, time: 1.day.ago)
Returns the value of tsv column or throw an exception if the column is not defined.
Setter for tsv column.
Returns the column object used to manage tsv field
See Lustra::Model::Column
Set the fields from json passed as argument and call save on the object
Trusted flag set to true will allow mass assignment without protection, FALSE by default
Set the fields from json passed as argument and call save! on the object
Trusted flag set to true will allow mass assignment without protection, FALSE by default
Generate the hash for update request (like during save)
Returns the value of updated_at column or throw an exception if the column is not defined.
Setter for updated_at column.
Returns the column object used to manage updated_at field
See Lustra::Model::Column
For each column, ensure than when needed the column has present information into it.
This method is called on validation.