class

Rome::Model

Inherits Rome::Serialization < Rome::Associations < Reference < Object

Constructors

create

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.

Source

Class methods

delete(*ids) : Nil

Deletes one or many records identified by ids from the database.

User.delete(1)
User.delete(1, 2, 3)
Source
update(id, args) : Nil

Updates one or many records identified by id in the database.

User.update(1, { name: julien })
User.update([1, 2, 3], { group_id: 2 })
Source
update(id, **args) : Nil

Updates one or many records identified by id in the database.

User.update(1, { name: julien })
User.update([1, 2, 3], { group_id: 2 })
Source

Instance methods

==(other : self) : Bool

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

Source
==(other) : Bool

Returns false (other can only be a Value here).

Source
[](attr_name : Symbol) : Value

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).

Source
[](attr_name : String) : Value

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).

Source
[]=(attr_name : Symbol, value : Value) : Value

Generic setter for an attribute. Only valid for known columns.

Source
[]?(attr_name : Symbol) : Value | Nil

Generic accessor for an attribute. Returns nil when the attribute is undefined (e.g. not loaded from the database).

Source
[]?(attr_name : String) : Value | Nil

Generic accessor for an attribute or extraneous attribute. Returns nil when the attribute is undefined (e.g. not loaded from the database).

Source
attributes=(attrs : NamedTuple) : Nil

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"
Source
changed?

Returns true if any attribute has changed since this model was initialized or last saved.

Source
changes

Returns the list of changed attributes.

Source
changes_applied

Tell that all changes have been applied to the database, thus clearing all change information.

Source
clear_changes_information

Clears all dirty attribute information.

Source
delete

Deletes the record from the database. Marks the record as deleted.

Source
deleted?

Returns true if the record has been deleted from the database.

Source
destroy

Deletes the record and dependent associations from the database. Marks the record as deleted.

Source
id

Always returns this record's primary key value, even when the primary key isn't named id.

Source
id?

Same as id but may return nil when the record hasn't been saved instead of raising.

Source
new_record?

Returns true if the record hasn't been saved to the database, yet.

Source
persisted?

Returns true is the record is known to exist in the database (has either been loaded or saved and not deleted).

Source
reload

Reloads a record from the database. This will reset all changed attributes and all change information.

Source
restore_attributes

Restores all dirty attributes to their pristine value. Eventually clears change information.

Source
save

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
Source
to_h

Exports this record as a Hash.

Source
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.

Source

Macros

columns(properties, strict = false)

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).
Source
columns

See #columns(properties, strict).

Source