Setting up a Bookstore Project. Quick Start to learn Active Record Query Interface

Carlos Del Real
4 min readJan 15, 2021

#Ruby on Rails, #ActiveRecord, #webdevelopment, #backend, #api.

Starting a Ruby on Rails project requires a bunch of commands that you have to be familiar with to be effective. This is an example of the most common ones.

On the Ruby on Rails guide, it is proposed a Bookstore project to be used for presenting the Active Record Query Interface.

Since I spend some time creating the same project, I think is going to be a good idea to document the process.

To start is worth mentioning that you have to have installed on your system ruby along with ruby on rails. If you haven’t, here you can find a couple of articles about how to do it.

To double-check that you have installed ruby and Ruby on Rails on your system just type:

ruby -v
rails -v

This is what you will get, the installed ruby and Ruby on Rails versions.

Start Building the project!

To start your project run the rails new Bookstore -T command.

This is going to create a new Ruby on Rails project on the Bookstorefolder, the -T flag is going to be used to generate it without the testing suite, later we are going to use RSpec as the testing suite.

As you can check, a folder Bookstorewas created within the workspace that you ran the command into. this folder contains all the files that your ruby on rails project uses.

Now your project does not have a single model and we are going to tackle that soon but for now, we are going to install RSpec. to do that add the gem ‘rspec' line into your Bookstore/Gemfile file. double-check that you insert it inside the test group block.

navigate into the Bookstore folder by cd Bookstore/ and run bundle exec rspec --init this is going to generate a Bookstore/spec folder and some RSpec configuration files.

Up to this point, your project is ready to add the models and perform the corresponding migrations.

The Project Goal

To build the table structure is absolutely necessary to have clarity about the table relationships. to serve that purpose is presented the following Bookstore Diagram

The created_at and updated_at columns exist for each table but are not shown to simplify this diagram.

Generating the models

On each of the following segments are presented the terminal commands to generate the table relationships and the models.

Author

rails generate model author first_name:string last_name:string title:string

Book

rails generate model book title:string year_published:integer isbn:string 'price:decimal{10,2}' out_of_print:boolean views:integer supplier:references author:references

Customer

rails generate model customer first_name:string last_name:string title:string email:string visits:integer orders_count:integer lock_version:integer

Order

rails generate model order date_submitted:time status:integer 'subtotal:decimal{10,2}' 'shipping:decimal{10,2}' 'tax:decimal{10,2}' 'total:decimal{10,2}' customer:references

Review

rails generate model review title:string body:text rating:integer state:integer customer:references book:references

Supplier

rails generate model supplier name:string

Each one of these commands generates a migration file within the Bookstore/app/db/migrate folder, whose purpose in life is to generate the corresponding tables in the database. At the same time in the folderapp/models are generated the model files.

migration files created on the migration folder
model files created on the model's folder

Notice that not all the model's code is automatically generated and the model’s logic for each of the models has to be updated to the code that is presented in the Ruby on Rails Guides to achieve the intended behavior

This is the Bookstore project goal taken from the RailGuides Active Record Query Interface

Run the migrations

Finally, run the commandrails db:migrate

This will migrate the database, in simpler words is going to generate the database tables, fields, and relationships that meet the described behavior in the models.

If everything is fine you will see something like this in your terminal

successfully running the migrations.

You can download this code at The Bookstore Project

--

--

Carlos Del Real

I am Fullstack web developer with a diverse background former Geologist, Geotechnical Engineer, and Master Candidate on Geographic Information Science & Systems