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
Tagging posts

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

Rails hosting made simple

Deploy apps to servers that you own and control.

Move fast and fix things

Application monitoring that helps developers get it done.

Summary

Learn how to implement tagging functionality in a Rails app using SQLite JSON columns. We'll cover how to create tags for posts, enforce database constraints, and add model validations to ensure data integrity. By the end, you'll have a flexible tagging system, with the next step allowing users to define custom tags.

Video Transcript

Up next, I want to explore building more advanced and interesting features into our application. Even though we're using SQLite, there's still a pervasive myth that SQLite is just a toy database engine that can't support powerful features. While it may not have all the advanced capabilities of Postgres, it's still surprisingly robust and allows us to build feature-rich applications.

In this example, we’ll explore adding tags to our posts table. Even though SQLite doesn’t support native array columns, we can still achieve the same functionality by leveraging JSON columns.

Adding a Tags Column to Posts

To implement tags, we:

  • Add a tags column to the posts table using a JSON type.
  • Ensure it's never empty by setting a default value of an empty array.
  • Enforce database-level constraints with a check constraint to guarantee that the tags column only stores valid JSON arrays (not objects, strings, integers, or null values).

This setup ensures that our tags behave just like a native array column, but with SQLite compatibility.

Adding Model-Level Validations

For additional data integrity, we:

  • Validate that tags is always an array.
  • Ensure all elements in the array are strings.

These checks prevent users from accidentally adding invalid data, keeping our system clean and predictable.

Creating a User-Friendly Tagging Interface

With our database structure ready, we need to update our form to allow users to select tags when creating or editing a post.

  1. Use the collection_check_boxes helper in Rails to display tag options.
  2. Define a collection of available tags as an array of strings.
  3. Wrap checkboxes in a visually appealing label, ensuring:
  • Selected checkboxes are highlighted.
  • Users can select multiple tags per post.

Ensuring Tags Are Processed Correctly

Now, we update our controller to properly handle tags attributes when saving a pos we specify tags as an array parameter, which ensures Rails knows how to process array values correctly.

Now, when a user selects multiple tags and updates a post:

  • The selected tags remain checked when editing again.
  • The controller properly saves and retrieves tag data.

Displaying Tags on the Post Page

To show tags in the post’s view, we update our metadata partial:

  • Check if a post has tags before rendering.
  • Display each tag using a styled span.

However, after testing, we noticed an issue:

  • Rails' collection_check_boxes helper adds an empty string to the tags array when no tags are selected.
  • This unwanted value appears in our UI.

Fixing Empty Tag Values

To prevent empty values from being stored, we use Active Record’s normalizes helper to clean the tags array.

Apply compact_blank to remove:

  • Empty strings ("")
  • nil values

This ensures that only valid tag values are stored.

Final Testing: Ensuring Everything Works

Now, when we:

  • Add multiple tags and update a post, they save correctly.
  • Remove all tags and update a post, they are cleared properly.

This gives us a fully functional tagging system using a simple JSON column.

Enhancing the Tagging System

  • Current feature: Users can select predefined tags from a static list.
  • Next step: Allow users to create custom tags dynamically.

In the next video, we’ll expand on this feature by implementing a more flexible tagging system, using higher-level SQL techniques.