ActiveRecord.Base class
Part of Active Record
Base class for ActiveRecord database-tied objects. These objects are also referred to as "models".
Summary
Methods
Query Engine
Database Relations
Methods
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
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)
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
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.
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:373Internal
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
1index of the relation to fetch - offset Number optional, defaults to
0amount 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:340Internal
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)
Returns all of the objects stored in the database.
Returns
- ActiveRecord::Base query object
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.
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
Runs the query. Use this function to actually launch your queries.
Object:all():get(function(results) ... end):fetch()
Returns
- ActiveRecord::Base query object
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
nilpassed to #expect, receives the object that was found
Returns
- ActiveRecord::Base query object
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
nilpassed to #expect, receives the object that was found
Returns
- ActiveRecord::Base query object
Returns the first object stored in the database.
Returns
- ActiveRecord::Base query object
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
Returns the last object stored in the database.
Returns
- ActiveRecord::Base query object
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
Inserts an ORDER BY condition into the query.
Object:order('id', 'asc')
Parameters
- column String column to sort by
- direction String optional, defaults to
nillowercase 'asc' or 'desc'; sorts in ascending order if omitted
Returns
- ActiveRecord::Base query object
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
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)
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)
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
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
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.
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
falsetrue if the parent has only one of this object, rather than many
Returns
- ActiveRecord::Base(self)
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)
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)
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)
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'
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)