Restrict Destroying Dependent Rails Associations with Error The dependent: :restrict_with_error option is a simple way to enforce data integrity in Rails apps. By preventing deletions of parent records with existing associations and providing helpful errors, it ensures historical data remains intact while guiding users on how to handle dependencies properly.
Backup SQLite from a Containerized Rails App Deployed with Kamal This post walks through backing up a SQLite database inside a Docker container on a remote server, specifically for a Ruby on Rails application deployed with Kamal. We'll begin with the basic commands, then convert them into a reusable shell script and a Rake task for convenience.
Working with the Rails Instrumentation API The Instrumentation API in ActiveSupport serves a dual purpose. You can use it to implement the observer (pub-sub) pattern, as well as benchmark how long it took to execute some action. In this post, we'll learn almost everything you need to know about the Rails Instrumentation API.
How a Ruby Method Becomes a Rails Action: Part One In this post, we will explore how a simple Ruby method, when added to a controller, becomes an action in Rails, ready to process incoming HTTP requests and send responses. We'll also trace the path of an incoming HTTP request to a Rails controller action.
Using Hotwire for Inline Form Updates Without Form Submission Sometimes you need dynamic content on the form, where parts of the form update based on the user input. This post shows how to use Hotwire's Turbo and Stimulus libraries for in-place form updates by loading data from the server, without submitting the form, which happens only once, at the end.
A Brief Introduction to Rails Initializers: Why, What, and How At first glance, Rails initializers seem complex, but they're solving a simple, but important problem: run some code after framework and gems are loaded, to initialize the application. This post covers the basics of initializers, including what they are, how they work, and how Rails implements them.
Not-Null Shortcut in Rails 8 Migration Generator Rails 8 lets you mark a database column as not-null by appending an exclamation mark after the column while generating a migration. It's a nice quality of life improvement. This post also contains a few things I learned after reading the pull request.
If You're a WordPress Developer, Learn Ruby and Rails With the recent WordPress drama, many developers might be concerned about its future. While WordPress isn't likely to go away, now might be the perfect time to invest in learning other tools and technologies. I suggest Ruby on Rails - you won't regret it!
How to Count the Number of Commits After a Specific Commit in Git Counting the number of commits after a specific commit in Git is a common task when you make small but frequent commits and need to squash them before rebasing from the main branch. This post shows one simple way to do this in git. Let me know if you know a better solution.
The Rails Router Handbook For the past few days, I've been trying to learn everything I could about the Rails router. I compiled all of my notes together with the past articles on this blog, and published a handbook on the router. I hope you find it useful and you learn a thing or two about the incredible Rails Router.
Routing Concerns in Rails You must have used concerns in Rails. Did you know you can also use concerns for your routes? They allow you to declare common routes to be reused in other resources and routes. This post covers the basics of routing concerns, including what they are, how they work, and when you might need them.
Understanding Shallow Nested Routes in Rails 💡This post is part of my handbook on the Rails Router.The Rails RouterEverything you need to know about the incredible routing system in Rails.