Member-only story

Getting Started With Rails Part 2

Steven Klavins
3 min readMay 11, 2020

As mentioned in part 1 we now need to complete the code in order to make a new article. At this point, we need to make an article model and add a create action in order to add the article to our database. First I would, however, add a testing framework and follow the TDD principles, I made a guide for RSpec Rails here.

In order to create the database model, I ran the following command

$ rails generate model Article title:string text:text

Why is the title a string and the body text? String translates to “Varchar” in your database, while text translates to “text”. Varchar values contain small chunks of data (for example a title) and text can be of almost any length.

This is an amazing feature of rails being it will automatically map out our database for us. If you navigate to the db/migrate we can see what Rails has done, as you can see it has created a method called change, it contains articles (plural, we can place article objects inside) with the two fields of an article (our title string and text article text).

Follow this we run the command $ rails db:migrate this command runs this change method and modifies the database accordingly. Like magic we now have a schema file with all of our changes applied, now all we need to do is to make a create action in order to pass the fields to…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Steven Klavins
Steven Klavins

Written by Steven Klavins

Hi, I’m Steven, most call me Steve! I’m a programmer, musician, and artist. This blog contains various tutorials and posts related to software development.

No responses yet

Write a response