abstract class Rome::Model
- Rome::Model
- Reference
- Object
Included Modules
Extended Modules
Defined in:
model.crpersistence.cr
Constructors
-
.create(**args) : self
Creates a record in database with the specified attributes.
Class Method Summary
-
.delete(*ids) : Nil
Deletes one or many records identified by ids from the database.
-
.update(id, args) : Nil
Updates one or many records identified by id in the database.
-
.update(id, **args) : Nil
Updates one or many records identified by id in the database.
Instance Method Summary
-
#==(other : self) : Bool
Returns
true
if this reference is the same as other. -
#==(other) : Bool
Returns
false
(other can only be aValue
here). -
#[](attr_name : Symbol) : Value
Generic accessor for an attribute.
-
#[](attr_name : String) : Value
Generic accessor for an attribute or extraneous attribute.
-
#[]=(attr_name : Symbol, value : Value) : Value
Generic setter for an attribute.
-
#[]?(attr_name : Symbol) : Value | Nil
Generic accessor for an attribute.
-
#[]?(attr_name : String) : Value | Nil
Generic accessor for an attribute or extraneous attribute.
-
#attributes=(attrs : NamedTuple) : Nil
Sets many attributes at once.
-
#changed? : Bool
Returns true if any attribute has changed since this model was initialized or last saved.
-
#changes : Hash(Symbol, Tuple(Rome::Value, Rome::Value))
Returns the list of changed attributes.
-
#changes_applied : Nil
Tell that all changes have been applied to the database, thus clearing all change information.
-
#clear_changes_information : Nil
Clears all dirty attribute information.
-
#delete : Nil
Deletes the record from the database.
-
#deleted? : Bool
Returns true if the record has been deleted from the database.
-
#destroy : Nil
Deletes the record and dependent associations from the database.
-
#id
Always returns this record's primary key value, even when the primary key isn't named
#id
. -
#id?
Same as
#id
but may returnnil
when the record hasn't been saved instead of raising. -
#new_record? : Bool
Returns true if the record hasn't been saved to the database, yet.
-
#persisted? : Bool
Returns true is the record is known to exist in the database (has either been loaded or saved and not deleted).
-
#reload : self
Reloads a record from the database.
-
#restore_attributes : Nil
Restores all dirty attributes to their pristine value.
-
#save : Nil
Persists the record into the database.
-
#to_h : Hash
Exports this record as a Hash.
-
#update(**attributes) : self
Updates a record into the database, optionally setting the specified attributes if present.
Macro Summary
-
columns(properties, strict = false)
Declares the model schema.
- columns(**properties)
Instance methods inherited from module Rome::Serialization
attributes=(pull : JSON::PullParser) : Nil
attributes=,
to_json(io : IO, indent = nil) : Nilto_json(builder : JSON::Builder) : JSON::Builder
to_json(indent = nil) : String to_json
Constructor methods inherited from module Rome::Serialization
new(pull : JSON::PullParser) : self
new
Constructor Detail
Creates a record in database with the specified attributes. For example:
user = User.create(name: "julien", group_id: 2)
# => User(id: 1, name: "julien", group_id: 2)
Automatically fills the created_at
and updated_at
columns if they
exist and are nil
.
Class Method Detail
Deletes one or many records identified by ids from the database.
User.delete(1)
User.delete(1, 2, 3)
Updates one or many records identified by id in the database.
User.update(1, { name: julien })
User.update([1, 2, 3], { group_id: 2 })
Updates one or many records identified by id in the database.
User.update(1, { name: julien })
User.update([1, 2, 3], { group_id: 2 })
Instance Method Detail
Returns true
if this reference is the same as other. Invokes same?
.
Returns false
(other can only be a Value
here).
Generic accessor for an attribute. Only valid for known columns. Raises a
Rome::MissingAttribute
exception when the attribute is undefined (e.g.
not loaded from the database).
Generic accessor for an attribute or extraneous attribute. Raises a
Rome::MissingAttribute
exception when the attribute is undefined (e.g.
not loaded from the database).
Generic setter for an attribute. Only valid for known columns.
Generic accessor for an attribute. Returns nil
when the attribute is
undefined (e.g. not loaded from the database).
Generic accessor for an attribute or extraneous attribute. Returns nil
when the attribute is undefined (e.g. not loaded from the database).
Sets many attributes at once. For example:
user = User.find(1)
user.attributes = {
group_id: 2,
name: "julien",
}
user.group_id # => 2
user.name # => "julien"
Returns true if any attribute has changed since this model was initialized or last saved.
Returns the list of changed attributes.
Tell that all changes have been applied to the database, thus clearing all change information.
Deletes the record and dependent associations from the database. Marks the record as deleted.
Always returns this record's primary key value, even when the primary key
isn't named #id
.
Same as #id
but may return nil
when the record hasn't been saved
instead of raising.
Returns true is the record is known to exist in the database (has either been loaded or saved and not deleted).
Reloads a record from the database. This will reset all changed attributes and all change information.
Restores all dirty attributes to their pristine value. Eventually clears change information.
Persists the record into the database. Either creates a new row or updates an existing row.
user = User.new(name: "julien", group_id: 2)
user.save # => INSERT
user.name = "alice"
user.save # => UPDATE
Updates a record into the database, optionally setting the specified attributes if present.
Automatically updates the updated_at
column if it exists.
Raises a ReadOnlyRecord
exception if the record has been deleted.
Macro Detail
Declares the model schema. For example:
class User < Rome::Model
columns(
id: {type: Int32, primary_key: true},
group_id: {type: UUID, null: true},
name: {type: String},
)
end
Available options:
type
—the column type (required);primary_key
—whether the column is the table primary key (default: false);null
—whether the column accepts NULL values (default: false).converter
—specify a converter type that must implement.from_rs(DB::ResultSet)
and should implement.from_json(JSON::PullParser)
.