Set up Rails Model

Tanuka Das
2 min readDec 16, 2019

In my previous blog Rails API From Scratch, I talked about the steps to create a Rails API. Here, we turn this API into a functional Rails app.

A model is a description of what the app is and what it can do. Basically it defines the app.

Step 1: generate a Rails model

It generates a model called Hat_api, added attributes during the generation. This Hat_api has a brand, which is string, a description is a text, and the price is decimal. The difference between text and string is a text can be very very long and a string is limited around 200 characters. We can always add more attributes if needed. Once you run this generator it creates a migration file, which is an instruction for the application to update the database and it creates the definition of the model, in this case, hat_api.rb.

This generates a new file in the db/migrate file. This new file holds instructions to update the database. There are fields which the rails application adds, as defined in the syntax, brand, description, and price. It also has a timestamp; this generates in two fields, created at and updated at and every time we change the records, update and create fields will be affected. Rails also create a unique id for every record in the database.

Step 2: Connect the PostgreSQL database to the Rails app

Run this only if you’re using the PostgreSQL database.

Step 3: Create backend

This creates the backend in the schema folder which holds a table called hat_api that has the brand, description, and price of the hats.

This is how a model is created in rails. Just to reiterate, a model defines what something is and what abilities does it have.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Tanuka Das
Tanuka Das

Written by Tanuka Das

Software Developer, technical writer, bookworm, constant learner.

No responses yet

Write a response