Improvements to in_order_of active record query method in Rails 7.1

Rails 7 has introduced the in_order_of method which allows to sort records in a specific order. There is a detailed explanation of it in the article Rails 7 adds in_order_of for ActiveRecord::QueryMethods and Enumerable. This is a follow-up article to that one, in which we will explore how the in_order_of method has been improved in Rails 7.1. Rails 7 adds in_order_of for ActiveRecord::QueryMethods and EnumerableRails 7 has added in_order_of method for ActiveRecord::QueryMethods and Enumerable to retrieve the data in an explicit order.Kiprosh BlogsSupriya Laxman MedankarAllows

Autoloading pitfalls fixed by Rails 7’s default Zeitwerk mode

Rails 7 onward zeitwerk mode completely replaces classic autoloader thereby resolving the flaws of classic autoloader

Rails 7 adds in_order_of for ActiveRecord::QueryMethods and Enumerable

Quite often in our Rails application, we need to retrieve records sorted in a specific order. For example, in an e-commerce application, we want to retrieve the customer orders in the order of "Completed, Cancelled, In Transit, Pending" statuses. The way to do it is to write an explicit SQL query. Starting Rails 7, this can be achieved simply with the help of the in_order_of method for ActiveRecord::QueryMethods and Enumerable. This article explains how we can use the in_order_of method to sort data in Rails 7. 1. ActiveRecord::QueryMethods#in_order_ofConsider we have a

Rails 7 supports tracking of belongs_to association

One of the most convenient features of Rails is the ability to track attribute changes. Rails 7 is releasing soon and bringing in a lot of new features as a treat for developers. One of the many features that Rails 7 is introducing, is that we can also track the associated object. This Rails ActiveRecord specific PR has added two methods for tracking the changes for the belongs_to association. In this article, we will discuss these two new methods with the help of examples. 1. association_changed?The association_changed? method tells if a different associated object has been

Sync files to a remote location using rsync linux command and Ruby

Note: The entire approach highlighted in this blog about using the rsync command with Ruby is only applicable on Linux-based systems and won't be available on Windows. In this article, we will explore how to use rsync in Ruby to sync files from local to a remote location, how to improve its error logging and how rsync is better than scp. Let's first understand what is rsync: rsync is a Linux command to sync files from one location to another. We can run this command on the terminal and it will copy files from one directory to another, either locally

Sign API request using Postman pre-request scripts

When we are about to test a dynamic API request via Postman and realize that we need to pass a freshly generated signature in the headers, what do we generally do? For each request, we generate the signature using server-side code and copy-paste it in the headers. Sounds like a tedious job, doesn’t it? We faced a similar situation in our application where we had to pass a signature in the headers while testing an API. The signature was generated by encoding all the request parameters using the user's secret key. If any of the request parameters changed, we

How to store emoji characters in MySQL database of a Ruby on Rails application

Emoji has become an essential part of communication in our digital lives. Hence, as developers, our applications should provide first-class support to them. In this blog post, we will explore how to store Emoji's in MySQL database. In one of our internal Ruby on Rails web application using MySQL database, we were facing an issue whenever a user tried to insert an emoji in the text of an article. The "save" functionality in UI ran infinitely (ajax) and logs were reporting following errors: ActiveRecord::StatementInvalid (Mysql2::Error: Incorrect string value: '\xF0\x9F\x98\x8A ...' for column 'content' at

Send Notifications on Slack using Incoming Webhook

Slack API provides Slack Incoming Webhook to easily send data on slack in real time. We just need to follow some steps and we are ready to send messages on slack. Setup Slack Incoming Webhook Integration into the slack workspace: On page we can see the form to create Incoming Webhook Integration. Select channel on which you want to send a notification from the dropdown or you can create a new channel by clicking on the link create new channel. Configuration Setting: After submitting the form you will redirect to page Edit Configuration. On this page, We can see Setup

Integrate Google Search Console with your site

What is search console? Google Search Console is a free service offered by Google that helps you monitor and maintain your site's presence in Google Search result. You don't have to sign up for Search Console for your site to be included in Google's search results, but doing so can help you understand how Google views your site and optimize its performance in search results. The tool gives webmasters the ability to find out if there are any specific problems that could be detrimental to being listed properly in the search results. These problems could be about how secure the

Rubyzip - create zip archive to send as attachment

In one of app we are sending multiple PDFs as attachment, so instead of sending individual file we decided to send all PDFs in zip. I found a gem 'Rubyzip', to create temporary zip archive and send it as attachment. Rubyzip is a ruby liabrary to reading and writing zip file. To create a zip and send it as attachment using Rubyzip here’s a simple example: Add gem in gemfile gem 'rubyzip' Write function to create zip file: def get_zip_data zip_file = Tempfile.new("example.zip") file_names = ['demo1.png', 'temp1.txt', 'temp2.pdf'] file_