abstract class DBX::ORM::Model

Overview

Base class for all models.

Example, creation of a model for a table users:

class User < DBX::ORM::Model
  adapter :pg

  class Schema # < DBX::ORM::Schema#(User)
    field id : Int64?
    field name : String
    field about : String
    field age : Int32
  end
end

Customize ModelQuery used by User model:

class User < DBX::ORM::Model
  # ...

  class ModelQuery < DBX::ORM::ModelQuery(User)
    # Custom select
    def select_custom
      self.select({:id, :name, :about, :age})
    end
  end
end

In the model example above, we have added a new method (select_custom) to `ModelQuery', which can be used in each query.

user = User.find(id).select_custom.to_o
users = User.find.select_custom.to_a

Defined in:

orm/model.cr

Class Method Summary

Class Method Detail

def self.connection : String #

DB connection name used by this Model instance.


[View source]
def self.db : DB::Database #

Returns DB connection name used by this Model instance.


[View source]
def self.models #

Returns array of all non-abstract subclasses of *DBX::ORM::Model.

DBX::ORM::Model.models # => [Contact, Address, User]

[View source]