# ActiveRecord Mechanics

## Objectives

1. Understand the connection between an ORM and Active Record
2. Understand why Active Record is useful
3. Develop a basic understanding of how to get started with Active Record

## ORM vs Active Record

By now you are familiar with the concept of an [ORM](http://en.wikipedia.org/wiki/Object-relational_mapping), an Object-Relation Mapper, and should have written something of your own in the `Student` and `InteractiveRecord` classes. Our latest iteration was our most powerful yet, it could give us lots of functionality via inheritance.

While building your own ORM for a single `Class` is a great way to learn about how object-oriented programming languages commonly interact with a database, imagine you had *many* more classes. To test and maintain custom code for each project we work on would distract our attention from making cool stuff to building database connectivity. To save themselves and other developers this headache, the [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html) Ruby gem team built the [ActiveRecord](http://guides.rubyonrails.org/active_record_basics.html) gem.

In this lesson we'll read about how to to have `ActiveRecord` link our Ruby models with rows in a database table. We won't write the code yet, but we'll familiarize ourself with common code blocs used in `ActiveRecord`-using projects.

## Active Record ORM

Active Record is a Ruby gem, meaning we get an entire library of code just by running `gem install activerecord` or by including it in our `Gemfile`.

### Connect to DB

Once our Gem environment knows to put `ActiveRecord` into the picture, we need to tell `ActiveRecord` where the database is located that it will be working with.

We do this by running `ActiveRecord::Base.establish_connection`. Once `establish_connection` is run, `ActiveRecord::Base` keeps it stored as a class variable at `ActiveRecord::Base.connection`.

> **NOTE**: If you'd like to type along in an IDE environment, you can experiment by using IRB with: `irb -r active_record` provided you've installed `ActiveRecord` with `gem install activerecord`

```
ActiveRecord::Base.establish_connection(
  :adapter => "sqlite3",
  :database => "db/students.sqlite"
)
```

### Create a table

But our database is empty. Let's create a table to hold students.

Let's create our table using SQL:

```
sql = <<-SQL
  CREATE TABLE IF NOT EXISTS students (
  id INTEGER PRIMARY KEY,
  name TEXT
  )
SQL

# Remember, the previous step has to run first so that `connection` is set!
ActiveRecord::Base.connection.execute(sql)
```

### Link a Student "model" to the database table `students`

The last step is to tell your Ruby class to make use of `ActiveRecord`'s built-in ORM methods. With Active Record and other ORMs, this is managed through [Class Inheritance](http://rubylearning.com/satishtalim/ruby_inheritance.html). We simply make *our* class (`Student`) a subclass of `ActiveRecord::Base`.

```
class Student < ActiveRecord::Base
end
```

Our `Student` class is now our gateway for talking to the `students` table in the database. The `Student` class has gained a whole bunch of [new methods](http://guides.rubyonrails.org/active_record_basics.html#creating-active-record-models) via its inheritance relationship to `ActiveRecord`. Let's look at a few of them

#### `.column_names`

Retrieve a list of all the columns in the table:

```
Student.column_names
#=> [:id, :name]
```

#### `.create`

Create a new `Student` entry in the database:

```
Student.create(name: 'Jon')
# INSERT INTO students (name) VALUES ('Jon')
```

#### `.find`

Retrieve a `Student` from the database by `id`:

#### `.find_by`

Find by any attribute, such as `name`:

```
Student.find_by(name: 'Jon')
# SELECT * FROM students WHERE (name = 'Jon') LIMIT 1
```

#### `attr_accessors`

You can get or set attributes of an instance of `Student` once you've retrieved it:

```
student = Student.find_by(name: 'Jon')
student.name
#=> 'Jon'

student.name = 'Steve'

student.name
#=> 'Steve'
```

#### `#save`

And then save those changes to the database:

```
student = Student.find_by(name: 'Jon')
student.name = 'Steve'
student.save
```

Note that our `Student` class doesn't have any methods defined for `#name` either. Nor does it make use of Ruby's built-in `attr_accessor` method.

```
class Student < ActiveRecord::Base
end
```

## Conclusion

You've now seen how `ActiveRecord` creates a link between Ruby and databases.

View [Active Record Mechanics (CRUD)](https://learn.co/lessons/active-record-mechanics-crud) on Learn.co and start learning to code for free.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://certil-remy.gitbook.io/learn/orm-and-active-record/untitled-11.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
