Generate a model
Active Record is a feature of Rails that maps relational databases to Ruby code. It helps generate the structured query language (SQL) for interacting with the database like creating, updating, and deleting tables and records. Our application is using SQLite which is the default for Rails.
Let’s start by adding a database table to our Rails application to add products to our simple e-commerce store. Run the following command in the terminal:
$ bin/rails generate model Product name:string
This command tells Rails to generate a model named Product
which has a name
column and type of string
in the database. Later on, you’ll learn how to add
other column types.
You should see the following in your terminal:
invoke active_record create db/migrate/20250526151900_create_products.rb create app/models/product.rb invoke test_unit create test/models/product_test.rb create test/fixtures/products.yml
This command does several things. It creates…
- A migration in the folder.
- An Active Record model in .
- Tests and test fixtures for this model.
Files
Preparing Environment
- Preparing Ruby runtime
- Create development database