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
The last advanced feature we're adding to our Lorem News application is site-wide search, powered by SQLite's Full-Text Search (FTS) engine.
Now, let me ask you—would a "toy database" include a powerful and fully-featured full-text search engine out of the box? I don’t think so, but SQLite does, and it comes built-in. The official documentation is linked below, and I highly recommend checking it out. Even after working with SQLite for years, I was surprised at how much power, functionality, and flexibility is packed into its full-text search capabilities.
We’re going to build our site-wide search using SQLite’s native full-text search engine. Even so, we’re only scratching the surface, covering about 10-15% of what’s possible.
To start, we need to create a migration that sets up a virtual table for full-text search.
After running the migration, we now have our virtual table in place.
Next, we need to create a PostDocument Active Record model to handle search indexing and queries.
One key thing to note: Active Record models don’t have to be backed by a physical table—they can also be backed by virtual tables like our FTS5 index.
We define two class methods:
Since our virtual table doesn’t store post content directly, we fetch the actual post records by joining results with our posts table.
To allow our Post model to perform searches, we define a class-level search method.
Here’s how it works:
This efficiently ranks posts based on how well they match the search query.
Now that our backend is ready, we need to wire up the frontend.
To improve user experience, we:
With just a few refinements, the search UI now looks clean and functions smoothly.
Let’s do a quick test:
Everything is working exactly as intended!
This completes the search feature in our Lorem News application! 🚀