Ruby on Rails – tables/databases

If you have a table that will be holding a bunch of articles for a blog, the title of that table is articles.  The model name will be the singular, so Article (and it should be capitalized).

Table = plural, lowercase of the model name => articles
Model = singular, first letter capitalized of the table name => Article
Model file name = should be lowercase, singular with .rb at the end => article.rb
Controller name = plural of model file name => articles_controller.rb
Filename should always be snake_case.
Model & class names should be CamelCase.

To generate a table:
rails generate migration create_articles

This creates a table in your alpha-blog > db > migrate folder called (a bunch of numbers)_create_articles.rb.  When you open this up you’ll see a table, but you need columns.  It has the code:
create_table :articles do |t|
To add a column and put the header of “title” on it, we need to add this code:
create_table :articles do |t|
  t.string :title

You could also have added a column to hold the article’s text (or description) by doing this code:
create_table :articles do |t|
  t.string :title
  t.text :description

but we’ll leave it out and add it later to show what to do in case you need to add a column once a table has been created.

We then need to run rails db:migrate to actually create the table.  Once that is done you’ll see code in db > migrate > schema.rb that shows the “articles” table. But we forgot to add the description column, so we have a few options of how to fix that.  I can run rails db:rollback which essentially is like command-z.  I can then add the description and run rails db:migrate again.  Even though this works, it isn’t the best way to deal with a mistake.  Instead, you should create a new migration file. To do this, enter rails generate migration add_description_to_articles.  Once this new migration is created, you need to open it: db > migrate > (bunch of numbers)_add_description_to_articles.rb and add some code to create columns.
def change
  add_column :articles, :description, :text
  add_column :articles, :created_at, :datetime
  add_column :articles, :updated_at, :datetime

What this just did was added 3 columns to our table titled “Description”, “Created At”, and “Updated At”.
Once this is done, you need to migrate the db again. rails db:migrate.

Once that is finished, you can go back to schema.rb and see the changes that occured.  We’ve created the database, but we need a model to be able to communicate with the database.  This is the image used in the course to help understand the relationships between all the pieces.

image from the Complete Ruby on Rails Developer Course – Mashrur Hossain

So we need to create a model to communicate with the database.  That is done under app > models > create new file called article.rb.  In that new file, you’ll need to put the code:
class Article < ApplicationRecord
end

Rails magically creates getters and setters for each of the columns we made earlier in the table once we make this model.

You can check to make sure this worked from rails console. This gives you direct access to your database. You can use it as a playground to test things out. If you type in Article.all it will show you if there is a connection. You can then type Article to see all the attributes. There are a lot of things you can do from here, but they are referenced in the video of section 4, lecture 46 at around 15:30.  But a few of the commands are:
article = Article.new
article.title = "This is my first article"
article.description = "This is the description
article.save
Another way to do this more succinctly is:
article = Article.new(title: "This is my 2nd article.", description: "This is the 2nd description." and then save.
Article.create(title: "This is my 3rd article.", description: "My 3rd description.") – this one you don’t have to save as the create method automatically saves it. This is probably the most effective method.
To get out of the rails console, just type exit.