The ultimate guide to understanding ActiveRecord

This is a complete look at the framework of a program. Today, we are just focused on the Model, but it’s interesting to note how each part relates and connects to each other.

What does ActiveRecord do though?

Let’s talk convention

Model classes are singular and in CamelCase
Database tables are plural and in snake_case

Setting Up ActiveRecord

Accepts a hash as input where :adapter key must be a name of a database adapter in lower-case (in this example, “sqlite3”). The :database key is specified with a path to your database.

Creating ActiveRecord Models

Boat model is a subclass of ActiveRecord::Base.

Creating ActiveRecord Tables

Rake db:create_migration automatically creates a file with a class that is a subclass of ActiveRecord::Migration. If you make this manually, make sure to add the version of migration you are using (here it’s [5.0]). Rake also creates the change method, which allows you to go in and create a table. This is a proper representation of convention in how to create a table.

Reading and Writing Data with CRUD

Software Engineer