High Leverage Rails
Introduction
Introduction to this course
Why use Ruby on Rails
Why use SQLite
Ruby on Rails + SQLite
Powering Your App with SQLite
Creating tables
Timestamps
Column types
Typeof
Ruby types
Creating table introduction
Creating table advanced
Inserting data
Updating data
Upserting data
Reading data
Virtual columns
Enums
Introduction to JSON
Indexing JSON
JSON table functions
Building a Modern Rails Application
Creating a new Rails application
Installing Solid Queue
Installing Solid Cache
Installing Solid Cable
Dockerfile
Application overview
Authentication
Base styles
Registration
Scaffolding posts
Polishing posts
Scaffolding comments
Polishing comments
Polishing models
Polishing controllers
Creating new post
Updating post
Reviewing MVP
Tagging posts
Custom tags
Friendly URLs
Full text search
Deploying & Operating Your App
Backups
Check Litestream locally
Verifying backups
Deployment options
Deploying with Hatchbox
Deployment constraints
Vertical scaling
Database access
Migrations
Locked video

Please purchase the course to watch this video.

Video thumbnail
Building a Modern Rails Application
Scaffolding comments

Full Course

$
129
$179
USD, one-time fee
This course came at the perfect time. I’ve recently gotten back into Rails after an 18-year hiatus, and this was a perfect refresher and shows just how much you can accomplish with Rails right out of the box.
Garrett Winder
Garrett Winder

Move fast and fix things

Application monitoring that helps developers get it done.

Rails hosting made simple

Deploy apps to servers that you own and control.

Summary

Learn how to add comments to posts in a Rails application while ensuring proper database relationships. This video covers setting up migrations, defining associations, and enforcing deletion rules so that comments are automatically removed when a post or user is deleted. By the end, you'll have a fully integrated comment system that maintains data consistency and integrity.

Video Transcript

Now that we have posts fully set up in our application, it's time to scaffold the comments resource, allowing users to add comments under any post.

Let's return to our application and run another Rails generator. This time, we will scaffold a Comment resource.

Generating the Comments Resource

A comment has three key attributes:

  • It belongs to a post.
  • It belongs to a user.
  • It has a body (text) that is required.

Let’s generate this resource by running the following command:

bin/rails generate scaffold Comment post:belongs_to user:belongs_to 'body:text!'

As always, this command generates:

  • A migration to create the database table
  • A model with the necessary relationships
  • Resourceful routes
  • A controller with RESTful actions
  • View templates
  • Tests for validation and functionality

Setting Up the Comments Model

Let’s inspect the generated migration. This migration ensures:

  • Every comment belongs to a post and a user (with foreign keys).
  • The body field is required (null: false).

Let’s run our migrations to apply these changes.

Now that the comments table is created, we can commit our changes.

Defining Associations in Models

Next, we need to define the relationships in our models.

Comment Model (Generated by Rails)

class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :user
end

We also need to update the Post model to establish its relationship with comments:

class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy
end

Adding dependent: :destroy ensures that when a post is deleted, all its associated comments are also removed.

Similarly, we update the User model:

class User < ApplicationRecord
  has_many :posts, dependent: :destroy
  has_many :comments, dependent: :destroy
end

This ensures that when a user account is deleted, all their posts and comments are automatically removed.

Now that our comments resource is set up, we still need to customize it to fit the specific needs of Lorem News.