Why You Should Consider Using excluding Instead of where.not in ActiveRecord

In Ruby on Rails, ActiveRecord simplifies working with databases, and where.not is a common way to filter out records based on a condition. However, in some cases, using excluding — introduced in Rails 7 — can be a better alternative. Here’s why.

Exploring Ruby's Programming Paradigms

Ruby is known for its flexibility and simplicity, allowing developers to work with different programming paradigms, such as procedural, object-oriented, and functional. Each of these styles has its strengths and can be applied in Ruby on Rails projects to solve specific problems elegantly. In this post, we’ll explore each paradigm with practical examples.

Decoupling for Orthogonality in Ruby on Rails: Why It Matters

In the world of software engineering, building maintainable, scalable, and robust applications is a priority. Ruby on Rails, a popular framework, emphasizes conventions that simplify development, but this convenience can sometimes lead to tightly coupled code. Decoupling, the practice of reducing dependencies between different parts of your code, plays a vital role in improving orthogonality—a system’s ability to make changes in one part without impacting others. This blog post explores why decoupling is essential for achieving orthogonality in Ruby on Rails applications and how it benefits your codebase.

Safe Navigation Operator vs. Explicit Nil Check: Which is Better?

When writing Ruby code, one of the most common tasks is to handle potential nil values safely. Ruby provides a few ways to do this, and two approaches often come up: using the safe navigation operator(&.) and placing an explicit nil check first. In this post, we’ll compare both techniques and help you understand which one is best suited for your code.

The Secret to Consistent Data: Active Record Validations

Ever inherited a project with a database full of messy, inconsistent data? Maybe you’ve dealt with users submitting forms filled with errors or blank fields? That’s where active record validations come to the rescue, and trust me—they’re a must-have in every developer’s toolkit.

Best Practices for Adding a Boolean Column in Rails: A Case Study

When adding a boolean column to a table in a Rails application, one common question is whether to set a default value for it. Let’s explore why this choice matters, the best practices around it, and how it impacts data handling. Using a real-world example, we’ll take a look at a migration adding a women_only column to a trips table.

Kamal in Rails 8: Simplifying Deployment

Rails 8 has introduced many exciting features to streamline the development process, and one of the most significant is the default inclusion of Kamal 2. But what exactly is Kamal, and how does it simplify deployments? In this post, we’ll delve into this powerful deployment tool and explore its integration with Rails 8.

How to Skip Validations When Updating in Rails

Rails provides several methods to update records, but one that stands out for its simplicity and efficiency is update_attribute.

Rails 8: Key Features and Developer Benefits

Ruby on Rails 8 is shaping up to be one of the most exciting versions yet, introducing powerful features for both seasoned developers and those new to the framework. From supporting SQLite for production to native authentication and improved Progressive Web App (PWA) support, Rails 8 brings significant innovations to the table. Here’s a breakdown of some of the most impactful changes:

How to Use instance_exec in Ruby on Rails

Ruby is known for its flexibility and powerful metaprogramming capabilities. One such method that often comes up in Ruby development, particularly in Ruby on Rails applications, is instance_exec. It allows developers to execute a block of code within the context of an object’s instance, making it extremely useful for dynamically executing code within objects without exposing unnecessary details or methods.

Morphing With Turbo Streams

Turbo Streams provide a real-time, server-driven way to update Rails applications without complex JavaScript. One of the latest enhancements to Turbo Streams is the ability to use the method: :morph option in various actions, allowing more efficient DOM updates. This option can be used with actions like update, replace, and others, providing more control over how updates are applied.

3 Must-Know Module Extensions in ActiveSupport

In this post, we’ll explore some of the most useful core extensions in Rails that enhance how developers work with modules: delegate, concern, and the deprecated but still important alias_method_chain. These tools simplify method delegation, modularize code, and modify method behavior. While alias_method_chain is no longer in use, understanding its history is helpful for maintaining legacy code. We’ll break down how each of these methods works and why they’re valuable, especially when writing cleaner, more efficient Rails applications.

Migrations in Rails 8: Using the New Not Null Shortcut

In the latest version of Ruby on Rails (Rails 8), developers have been given a handy new shortcut for adding a NOT NULL constraint to database columns. This small but powerful enhancement simplifies the process of generating migrations, making them cleaner and more intuitive.

Using Active Record Store in Ruby on Rails

Active Record Store is a powerful feature in Ruby on Rails that allows you to store structured data in a flexible way. Instead of creating separate tables for every piece of information, you can store data as a hash directly in your model. This is especially useful for scenarios where the structure of the data may change over time or is not strictly defined.

Writing More Expressive Ruby with the it Shorthand

Ruby is a language that consistently evolves to make code more expressive, concise, and readable. With the release of Ruby 3.4, one of the exciting new features introduced is the use of it as a shortcut for the first parameter in a block. This enhancement aims to simplify code, especially in situations where blocks are used extensively, reducing the need to explicitly declare block parameters.

4 Tips to Resolve N+1 Queries in Ruby on Rails

In Ruby on Rails, N+1 query problems occur when your application makes an excessive number of database queries due to how associations between models are loaded. This can lead to severe performance degradation, especially as your data scales.

Positional and Keyword Arguments in Ruby on Rails

Method parameters can be passed in different ways, allowing for flexible and readable code. Understanding the difference between positional arguments and keyword arguments is essential for writing clean and maintainable code.

Understanding Struct in Ruby on Rails

Ruby is known for its elegant syntax and flexibility, which allows developers to create concise and readable code. One of the lesser-known but powerful tools in Ruby is Struct, a built-in class that provides a simple way to group related attributes without the overhead of defining a full class. While not as commonly used as ActiveRecord models or POROs (Plain Old Ruby Objects), Struct can be incredibly useful in Rails applications for certain scenarios.

Lazy Loading vs. Eager Loading

When working with Ruby on Rails, managing how your application loads data from the database is crucial for performance optimization. Two common techniques used to handle this are lazy loading and eager loading. Understanding the differences between them and knowing when to use each can help improve both the speed and efficiency of your Rails application.

Applying the Law of Demeter in Object-Oriented Programming with Rails

In object-oriented programming (OOP), adhering to principles that promote loose coupling and encapsulation is essential for building maintainable, scalable systems. One such principle is the Law of Demeter, also known as the Principle of Least Knowledge. This principle helps prevent objects from knowing too much about the internal workings of other objects, which is a common issue in OOP.

Mastering Rails Params: with_defaults

Handling parameters in Ruby on Rails is a common task, especially when working with forms and strong parameters. A lesser-known but powerful method that can simplify this process is with_defaults. This method allows you to provide default values for any missing keys in a hash, making your code cleaner and more efficient. Let’s explore how with_defaults works and how you can use it effectively with strong parameters.

Understanding HashWithIndifferentAccess in Ruby on Rails

In the Ruby on Rails framework, there’s a little-known feature called HashWithIndifferentAccess that can make handling hash keys more convenient and error-free. If you’ve ever found yourself frustrated by having to remember whether a hash key is a string or a symbol, this feature is for you!

Basic Array Methods: A Practical Guide

Arrays are one of the most versatile and commonly used data structures in Ruby. They allow you to store and manipulate collections of elements efficiently. Ruby provides a rich set of methods for working with arrays, making it easy to add, remove, sort, transform, and filter data. In this blog post, we’ll explore some essential Ruby array methods, complete with examples and practical applications.

ActiveSupport::Concern Explained

When building complex Ruby on Rails applications, code organization and modularization become crucial. As your application grows, so does the need to break down logic into smaller, manageable parts. This is where ActiveSupport::Concern comes into play.

Exploring the Power of none?

When working with arrays or enumerables in Ruby (and by extension, Rails), you often need to check whether certain conditions are met for the elements within a collection. The none? method provides a clean and expressive way to verify if no elements in a collection satisfy a condition. It’s especially useful when you want to ensure that a collection is either empty or that none of its elements match a particular condition.