Active Record Basics

When we ran the Rails model generator to create the Product model, it created a file at . This file creates a class that uses Active Record for interacting with our products database table.

class Product < ApplicationRecord
end

You might be surprised that there is no code in this class. How does Rails know what defines this model?

When the Product model is used, Rails will query the database table for the column names and types and automatically generate code for these attributes. Rails saves us from writing this boilerplate code and instead takes care of it for us behind the scenes so we can focus on our application logic instead.

Let’s use the Rails console to see what columns Rails detects for the Product model.

Run:

store(dev)> Product.column_names

And you should see:

=> ["id", "name", "created_at", "updated_at"]

Rails asked the database for column information above and used that information to define attributes on the Product class dynamically so you don’t have to manually define each of them. This is one example of how Rails makes development a breeze.

Powered by WebContainers
Files
Preparing Environment
  • Preparing Ruby runtime
  • Prepare development database