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_

Chartkick: To Make Easy Charts and Graphs with Rails.

Now a days in many website we use chart to show the statistics for the site and there are many plugins and gems available for it. But I liked one gem Chartkick that I used in one of our application to create column chart to show weekly, yearly and From date - ToDate range statistics of site. I found it very useful and easy to use. You will get details about chartkick here. To use chartkick you have to just follow simple step and you are ready to create beautiful Javascript charts with one line of Ruby. Chartkick can work

left outer join in Rails 5

In Rails 5 Active Record provide two finder methods to specify JOIN on associated tables, joins left_outer_joins Suppose in our application we have Article and Comment. Here comments belong to an article, while article have many comments. Now if we want to show list of all articles along with number of comments, we need join to Article and Comment by left_outer_join. In rails 4.x, Active Record does not have support for outer joins. So we need to add SQL for left outer join manually. articles = Article.join('LEFT OUTER JOIN "comments" ON "

Wicked PDF : PDF generation plugin

Wicked PDF uses shell utility wkhtmltopdf to serve a PDF file to a user from HTML. In other words, you simply write an HTML view as you would normally, then wicked PDF takes care of other stuff. wicked PDF works on Ruby versions 1.8.7 through 2.3; Rails 2 through 5.0 Installation: To use Wicked PDF, first install wkhtmltopdf. Alternatively, you could use the wkhtmltopdf-binary gem by including it in your Gemfile. Add following gems to your Gemfile and run bundle install. gem 'wicked_pdf' gem 'wkhtmltopdf-binary' Register the PDF mime type in config/initializers/mime_types.