Chapters
1. Creating Active Record Models
To create Active Record models, use the New
method from activerecord
package
and you’re good to go:
Product := activerecord.New("product")
Suppose that the products
table was created using an SQL statements like following:
CREATE TABLE products (
id INTEGER NOT NULL auto_increment,
name VARCHAR,
PRIMARY KEY (id)
);
The schema above declares a table with two columns: id and name. Each row of this table represents a certain product with these two parameters. Thus, you would be able to write code like the following:
p := Product.New()
p.AssignAttribute("name", "Some Book")
fmt.Println(p) // #<Product name: "Some Book">
2. Reading and Writing Data
2.1. Create
Active Record objects can be created from hash or have their attributes manually set after creation.
For example, using a Create
a new instance of the model will be created and inserted into
the database.
user = User.Create(activesupport.Hash{"name": "Eric", "occupation": "psychiatrist"})
Using New
method, a new instance of the model will be created without being saved.
user := User.New()
user.AssignAttribute("name", "Eric")
user.AssignAttribute("occupation", "psychiatrist")
2.2. Read
Active Record provides API for accessing data within a database. Below there are few examples of different approaches accessing data.
// return a collection of all users.
users := User.All()
// return a user with name "Eric".
user := User.Where("name", "Eric")
// return a user with id = 1.
user := User.Find(1)
2.3. Update
TBD.
2.4. Delete
Once retrieved, an Active Record instance can be removed from a database.
user := User.FindBy("name", "Eric")
user.Delete()
Collection of records can be destroyed using DestroyAll
method.
user := User.Where("occupation", "psychiatrist")
user.DeleteAll()