What is Active Record?
Active Record is what's called an object-relational mapper (ORM). In essence it acts as a go between or bridge between your app's objects and the database.
Why is it useful?
As fun as writing SQL can be, ActiveRecord's special syntax enables you to avoid having to write complicated queries. Instead it translates this syntax into the SQL for you.*
**Your ActiveRecord command**
```
Person.last
```
**The resulting SQL command**
```
SELECT `people`.* FROM `people` ORDER BY `people`.`id` DESC LIMIT 1
```
*You can still write SQL with ActiveRecord if you want or need to.
Two way street
ActiveRecord also connects data flowing from your app back to the database.
Do you have a form where users are signing up, creating a post, or adding a writing prompt?
**The ActiveRecord command**
```
Prompt.create(text: "This is a writing prompt example")
```
**The resulting SQL command**
```
INSERT INTO "prompts" ("created_at", "text", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", "2017-01-25 17:59:20.333595"], ["text", "This is a writing prompt example"], ["updated_at", "2017-01-25 17:59:20.333595"]]
```
How do I set it up for my Rails app?
- ActiveRecord is a gem that is included by default when you create a new Rails project
- In order to set up the connections between different tables in the app's database, you need to specify relationships
- You do this in your model and migration files
- These relationships enable you to use ActiveRecord to access say, the orders of a particular user, or even a more complex query like totalling up the profit made on orders where the product had the word "green" in its title somewhere