each_cons in Ruby on Rails: from simple to real-life examples

The each_cons method in Ruby on Rails is a versatile tool that simplifies iterating over consecutive pairs in a collection. With just a single line of code, we can perform tasks such as finding patterns or calculating differences between adjacent elements. In this blog post, we’ll explore the various use cases of each_cons, starting from simple examples and gradually progressing to more complex ones.

Exploring Multiple Database Support in Rails

Working with multiple databases is a common requirement in many Rails applications. Whether you need to interact with legacy databases, implement data sharding strategies, or separate different types of data, Rails provides robust support for managing multiple databases.

Migrating from Paperclip to Active Storage in Rails

Migrating from one file attachment library to another can be a challenging task, but with Rails’ built-in Active Storage, the process becomes much smoother.

Data.define in Ruby 3.2: An Introduction to Immutability

Ruby 3.2 introduces a new Data class to define immutable value objects that contain a simple set of values. Similar to the Struct class in Ruby, it provides a simpler and more restricted API. In this article, we’ll explore what the Data class is and how it can be useful from a practical perspective.

Securing Ruby on Rails Applications: Part 8 (Use Strong Encryption for Sensitive Data)

In today’s digital landscape, the protection of sensitive data is of paramount importance. Encryption plays a crucial role in ensuring the confidentiality and integrity of sensitive information within your Ruby on Rails application. By implementing strong encryption techniques, you can significantly enhance the security of data at rest and in transit. In this article, we will explore how to leverage strong encryption for sensitive data in a Ruby on Rails application, along with code examples to guide you through the process.

Securing Ruby on Rails Applications: Part 7 (​​Use Strong Password Policies)

In the digital age, ensuring the security of user accounts is paramount. One crucial aspect of account security is implementing strong password policies. By enforcing robust password requirements, you can significantly reduce the risk of unauthorized access and data breaches. In this article, we will explore how to implement strong password policies in a Ruby on Rails application, complete with code examples to guide you through the process.

Securing Ruby on Rails Applications: Part 6 (Use Two-Factor Authentication (2FA))

In an increasingly digital world, ensuring the security of user accounts has become a critical concern for web developers. One effective method to safeguard user data and prevent unauthorized access is through Two-Factor Authentication (2FA). This authentication process adds an extra layer of security by requiring users to provide two forms of identification before accessing their accounts. In this article, we will explore how to implement 2FA in a Ruby on Rails application, with code examples to guide you through the process.

Securing Ruby on Rails Applications: Part 5 (Sanitize User Input)

Ruby on Rails is a powerful web application framework that enables developers to build robust and scalable web applications. However, like any other web application, Rails applications are vulnerable to attacks if not properly secured. One of the most critical aspects of securing a Rails application is sanitizing user input.

Securing Ruby on Rails Applications: Part 4 (Use Secure Session Management)

Ruby on Rails is a powerful and popular web application framework that allows developers to build secure and scalable web applications. However, like any other web application, Rails applications are vulnerable to attacks if not properly secured. One of the most important aspects of securing a Rails application is secure session management.

Securing Ruby on Rails Applications: Part 3 (Use Strong Parameters)

Ruby on Rails is a popular web framework that is known for its security features. However, there are still many steps that developers need to take in order to ensure that their Rails applications are secure. One important step is to use strong parameters to protect against malicious input.

Securing Ruby on Rails Applications: Part 2 (Input Validation)

Input validation is an essential part of web application security, as it ensures that data entered by users is in the expected format and doesn’t contain any malicious content. In Ruby on Rails, input validation can be implemented using various methods and libraries. In this article, we’ll explore some of these techniques and provide code examples.

Understanding Ancestor Chains in Ruby

An ancestor chain in Ruby on Rails refers to the path of inheritance leading up to a given class or module. Understanding ancestor chains goes deeper into Ruby’s core functionality. By tracing the ancestor chain of a class or module, you can better understand its relationships to other parts of their application and to Ruby itself.

Securing Ruby on Rails Applications: Part 1 (Use Strong Authentication and Authorization Mechanisms)

Authorization is an essential aspect of web application security that ensures that only authorized users have access to certain resources or functionalities within the application. Ruby on Rails provides several mechanisms for implementing authorization, including role-based and attribute-based authorization. In this article, we will discuss how to implement authorization in Ruby on Rails, with code examples.

A Quick Guide to Basic Caching in Ruby on Rails

Caching is a useful method that can improve the performance of web applications. By caching frequently accessed data and pages it can make the application faster, can serve content more quickly resulting in a better user experience, and improve the scalability of the application by reducing the number of requests made to the database. In this blog post, we will discuss some commonly used caching methods in Ruby on Rails.

Boosting Your Rails Console With .irbrc

You’re likely familiar with the IRB (Interactive Ruby Shell) console, a powerful tool that enables you to quickly test and experiment with Ruby code. However, did you know that you can customize the IRB console by using a .irbrc file? This file is a Ruby script that executes every time a new IRB console is started. By using a .irbrc file, you can customize the IRB console, improve your productivity, and ensure consistency across your development environment.

Boosting Performance and Readability: ActiveSupport::ArrayInquirer vs. includes

ActiveSupport::ArrayInquirer is a handy utility class that allows you to easily work with arrays. It makes it easy to check whether an array includes a certain value without having to write complex code.

Rails routes table search

The Rails router handles incoming HTTP requests and maps them to the appropriate controller/action. Developers can define routes by adding them to routes.rb.

Ruby on Rails Log Levels: Examples and Best Practices

Logging is important in any software development project because it helps to understand how the application is behaving and identify issues when they occur. The errors of an application cannot all be treated in the same way, as they have different levels of importance. Depending on where the error occurs in your application, it can be fatal to a process or just a warning.

Rails default Dockerfile

Docker has become an industry standard. It allows developers to create, deploy, and run applications inside containers. In Rails 7.1, Dockerfiles are included by default. This makes it easier than ever to deploy Rails applications using Docker.

Showing All Headers in Action Mailer Previews

ActionMailer previews can now display all email headers. This allows developers to check all headers, when testing locally.

Using Signed Global IDs with Polymorphic Select Fields in Rails Forms

Polymorphic associations allow a single association to reference multiple models. In a polymorphic association, the association name is used to store the name of the associated model, and the _id and _type attributes are used to store the ID and type of the associated record. However, polymorphic associations can be challenging to work with when it comes to using select fields, especially when there are multiple types of associated models.

Using Date Ranges in Rails Queries

When working with dates in Rails, it’s common to want to find records that fall within a certain range of dates. For example, you might want to find all orders placed between two dates, or all events scheduled for a certain week.

Turbo Streams Duplicate Element When Appending/Prepending

In January, 2021 a bug was reported in Turbo that caused duplicate DOM elements. In this post we’ll explain the bug and how it was fixed.

ActiveRecord regroup method

ActiveRecord’s regroup method is a shorthand for using unscope to remove existing groups.

Rails counter available in collection partials

Rails makes a counter variable available within partials that are rendered with a collection. This variable follows the name of the collection. A @users collection has a counter called user_counter. The counter works like the index on an each loop. It counts the number of times a partial has been rendered. Let’s look at an example.