Base class for ActiveRecord database-tied objects. These objects are also referred to as "models".

Summary

Methods

When the ActiveRecord::Base class is extended, the new model is added to the global models list, and its table name is determined based on the class name.
dump()shared
Dump the object as a simple data table.
Returns the database schema, or attempts to get it from the global schema storage in case the object hasn't been properly initialized yet.
init()shared internal
Sets up basic variables and creates empty tables for relations that the object has many of.

Query Engine

_create_restored(data)shared internal
Internal function to create a new instance of the object based on the data from the database.
Internal function to fetch all relations when the objects are fetched from the database.
Internal function to process child objects or the current object as a child to another object.
all()shared
Returns all of the objects stored in the database.
destroy()shared
Deletes the current object from the database.
Used when a single return value is expected.
fetch()shared
Runs the query.
Finds an object in the database by its ID.
Finds an object in the database by a column value.
first()shared
Returns the first object stored in the database.
Used when multiple return values are expected.
last()shared
Returns the last object stored in the database.
Inserts a LIMIT condition into the query.
Inserts an ORDER BY condition into the query.
Used to catch errors during queries.
run_query(callback)shared internal
Runs the query based on the query map of the query object and flushes the query map.
save()shared
Saves the object to the database.
skip(amt)shared
Inserts an OFFSET condition into the query.
Specifies a WHERE condition in the query.
Specifies a WHERE NOT condition in the query.

Database Relations

Specifies that the object belongs to a parent object.
Specifies that the object has one or many of another object.
Specifies that the object has many instances of another object.
Specifies that the object has one instance of another object.
Callback that is called if the object's validation fails.
Specifies a validation for a column.

Methods

ActiveRecord.Base:class_extended(new_class)

shared internal packages/active_record/lib/base.lua:72

Internal

This function is internal. It is not part of the public API and may change without notice.

When the ActiveRecord::Base class is extended, the new model is added to the global models list, and its table name is determined based on the class name. Make sure you create a table that is named as a lowercase plural of the class name, or else this will fail!

Parameters

  • new_class ActiveRecord::Base the newly created model class

ActiveRecord.Base:dump()

shared packages/active_record/lib/base.lua:89

Dump the object as a simple data table. This simply dumps all the values of the variables defined in the schema.

Returns

  • Map schema
  • ActiveRecord::Base(self)

ActiveRecord.Base:get_schema()

shared packages/active_record/lib/base.lua:81

Returns the database schema, or attempts to get it from the global schema storage in case the object hasn't been properly initialized yet.

Returns

  • Map schema

ActiveRecord.Base:init()

shared internal packages/active_record/lib/base.lua:54

Internal

This function is internal. It is not part of the public API and may change without notice.

Sets up basic variables and creates empty tables for relations that the object has many of.

Query Engine

Provides utility functions for abstracted database querying.

ActiveRecord.Base:_create_restored(data)

shared internal packages/active_record/lib/base.lua:526

Internal

This function is internal. It is not part of the public API and may change without notice.

Internal function to create a new instance of the object based on the data from the database.

Parameters

  • data Map row returned by the database

Returns

  • ActiveRecord::Base(object)

ActiveRecord.Base:_fetch_relation(callback, objects, n, offset)

shared internal packages/active_record/lib/base.lua:373

Internal

This function is internal. It is not part of the public API and may change without notice.

Internal function to fetch all relations when the objects are fetched from the database. Each relation is loaded with one query for up to 500 of the objects at a time.

Parameters

  • callback Function called with the objects once all of their relations are loaded
  • objects List<ActiveRecord::Base> objects to fetch the relations of
  • n Number optional, defaults to 1 index of the relation to fetch
  • offset Number optional, defaults to 0 amount of objects whose relation n has been fetched already

Returns

  • ActiveRecord::Base(self)

ActiveRecord.Base:_process_child(obj, target_class)

shared internal packages/active_record/lib/base.lua:340

Internal

This function is internal. It is not part of the public API and may change without notice.

Internal function to process child objects or the current object as a child to another object.

Parameters

  • obj ActiveRecord::Base parent object
  • target_class ActiveRecord::Base class of the parent object

Returns

  • ActiveRecord::Base(self)

ActiveRecord.Base:all()

shared packages/active_record/lib/base.lua:249

Returns all of the objects stored in the database.

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:destroy()

shared packages/active_record/lib/base.lua:771

Once done, do not attempt saving the object again, since it will cause unpredictable behavior

Deletes the current object from the database.

obj:destroy()
-- do not use the object after this point.

ActiveRecord.Base:expect(callback)

shared packages/active_record/lib/base.lua:555

Used when a single return value is expected. Calls the callback with a single object when the object has finished loading from the database.

Object:first():expect(function(obj) ... end)

Parameters

  • callback Function receives the loaded object

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:fetch()

shared packages/active_record/lib/base.lua:597

Runs the query. Use this function to actually launch your queries.

Object:all():get(function(results) ... end):fetch()

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:find(id, callback)

shared packages/active_record/lib/base.lua:278

Finds an object in the database by its ID. Optionally can also call a callback right away.

Object:find(1)
Object:find(1, function(obj) ... end)

Parameters

  • id Number
  • callback Function optional, defaults to nil passed to #expect, receives the object that was found

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:find_by(column, value, callback)

shared packages/active_record/lib/base.lua:296

Finds an object in the database by a column value. Optionally can also call a callback right away.

Object:find_by('id', 1)
Object:find_by('id', 1, function(obj) ... end)

Parameters

  • column String
  • value Any value to look for, converted to a string
  • callback Function optional, defaults to nil passed to #expect, receives the object that was found

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:first()

shared packages/active_record/lib/base.lua:237

Returns the first object stored in the database.

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:get(callback)

shared packages/active_record/lib/base.lua:574

Used when multiple return values are expected. Calls the callback with all objects that were returned from the database once the object has finished loading.

Object:all():get(function(results) ... end)

Parameters

  • callback Function receives a List of the loaded objects

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:last()

shared packages/active_record/lib/base.lua:243

Returns the last object stored in the database.

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:limit(amt)

shared packages/active_record/lib/base.lua:312

Inserts a LIMIT condition into the query. The code in the example below will find the first 10 entries that have more than "100" money.

Object:where('money > 100'):limit(10)

Parameters

  • amt Number maximum amount of objects to load

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:order(column, direction)

shared packages/active_record/lib/base.lua:261

Inserts an ORDER BY condition into the query.

Object:order('id', 'asc')

Parameters

  • column String column to sort by
  • direction String optional, defaults to nil lowercase 'asc' or 'desc'; sorts in ascending order if omitted

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:rescue(callback)

shared packages/active_record/lib/base.lua:625

Used to catch errors during queries. This callback is called in case no object was found in the database. The callback's first argument is a new object of the same class pre-made for you.

Object:where('id > 100000'):first():expect(function(obj)
  ...
end):rescue(function(new_object)
  ...
end)

Parameters

  • callback Function receives a new, unsaved object of the same class

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:run_query(callback)

shared internal packages/active_record/lib/base.lua:458

Internal

This function is internal. It is not part of the public API and may change without notice.

Runs the query based on the query map of the query object and flushes the query map. The callback, the #rescue callback and the conditions are taken from the query object when the query is sent.

Parameters

  • callback Function called with a List of the loaded objects; not called if nothing was found

Returns

  • ActiveRecord::Base(self)

ActiveRecord.Base:save()

shared packages/active_record/lib/base.lua:695

Saves the object to the database. This is the function that actually commits data to the database.

local obj = Object.new()
  obj.money = 100
obj:save()

Returns

  • ActiveRecord::Base(self)

ActiveRecord.Base:skip(amt)

shared packages/active_record/lib/base.lua:327

Inserts an OFFSET condition into the query. The code in the example below will return the SECOND entry in the database:

Object:where('money > 100'):skip(1):limit(1)

Parameters

  • amt Number amount of objects to skip

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:where(condition, ...)

shared packages/active_record/lib/base.lua:139

Specifies a WHERE condition in the query. The values are passed to the database as bind parameters, so they need no escaping.

Object:where('column', 'value')
Object:where('column > ?', 100)
Object:where({ ['column'] = 'value', ['column2'] = { 'value', 'value2' } })

Parameters

  • condition String/Map column name, SQL condition (which may use ? placeholders), or a hash of column-value pairs in which a List value stands for IN (...)
  • ... Vararg value to compare the column with (nil stands for IS NULL), or the values that replace the ? placeholders

Returns

  • ActiveRecord::Base query object

ActiveRecord.Base:where_not(condition, ...)

shared packages/active_record/lib/base.lua:193

Specifies a WHERE NOT condition in the query. Works exactly the same as #where, except that it does the opposite.

Object:where_not('column', 'value')
Object:where_not('column > ?', 100)
Object:where_not({ ['column'] = 'value', ['column2'] = { 'value', 'value2' } })

Parameters

  • condition String/Map column name, SQL condition (which may use ? placeholders), or a hash of column-value pairs in which a List value stands for NOT IN (...)
  • ... Vararg value to compare the column with (nil stands for IS NOT NULL), or the values that replace the ? placeholders

Returns

  • ActiveRecord::Base query object

Database Relations

Provides functions to bind multiple database-tied objects together.

ActiveRecord.Base:belongs_to(target, one)

shared packages/active_record/lib/base.lua:885

Specifies that the object belongs to a parent object.

MyClass:belongs_to 'User'

Parameters

  • target String/ActiveRecord::Base class of the parent model, or its name (e.g. 'User')
  • one Boolean optional, defaults to false true if the parent has only one of this object, rather than many

Returns

  • ActiveRecord::Base(self)

ActiveRecord.Base:has(what, many)

shared packages/active_record/lib/base.lua:798

Specifies that the object has one or many of another object. The object(s) will be stored in a field with the same name as the child's database table.

MyClass:has('users', true)
...
MyClass:first():expect(function(obj)
  print(obj.users) -- table
end)

Parameters

  • what String/Map table name of the child model, or a hash such as { 'table_name', as = 'field_name' } to store the object(s) in another field
  • many Boolean true if the object has many of them, false if it has only one

Returns

  • ActiveRecord::Base(self)

ActiveRecord.Base:has_many(what)

shared packages/active_record/lib/base.lua:856

Specifies that the object has many instances of another object. The objects will be stored in a field with the same name as the child's database table.

MyClass:has_many 'users'
...
MyClass:first():expect(function(obj)
  print(obj.users) -- table
end)

Parameters

  • what String/Map table name of the child model, or a hash such as { 'table_name', as = 'field_name' } to store the objects in another field

Returns

  • ActiveRecord::Base(self)

ActiveRecord.Base:has_one(what)

shared packages/active_record/lib/base.lua:872

Specifies that the object has one instance of another object. The object will be stored in a field with the same name as the child's database table.

MyClass:has_one 'user'
...
MyClass:first():expect(function(obj)
  print(obj.user) -- #<User>
end)

Parameters

  • what String singular underscored name of the child model, e.g. 'ammunition'

Returns

  • ActiveRecord::Base(self)

ActiveRecord.Base:invalid(column, err_code)

shared packages/active_record/lib/base.lua:905

Callback that is called if the object's validation fails.

Parameters

  • column String column that failed the validation
  • err_code String id of the failed validation, e.g. 'presence'

ActiveRecord.Base:validates(column, options)

shared packages/active_record/lib/base.lua:919

Specifies a validation for a column.

MyObject:validates('email', { presence = true, uniqueness = true })

See all available validations in ActiveRecord::Validator.

Parameters

  • column String
  • options Map validation ids mapped to their settings, e.g. { min_length = 4 }; case_sensitive = true makes the uniqueness validation of the column compare case

Returns

  • ActiveRecord::Base(self)

Defined in