Rails set timezone per request

Sometimes we need to have a request be processed in a particular timezone in Rails. To set a timezone per request in Rails, we can have an around filter that sets timezone, runs the requests and then resets timezone to what it was before the request.

»
Author's profile picture Akshay Mohite on Rails |

Rails render 404 not found from a controller action

Rails redirects route not found requests to 404.html page. Sometimes, we need to redirect to 404 not found page from a controller action. This can be done by rendering 404.html page or raising ActionController::RoutingError error.

»
Author's profile picture Akshay Mohite on Rails |

Do not set Time.zone to avoid timezone issues in Rails

Setting Time.zone in a Rails application changes time zone globally. Whenever Time.now is used in any other request, it uses whatever Time.zone is set to earlier. To avoid Time.zone issues, do not set Time.zone. Instead, use in_time_zone method to get time in desired zone.

»
Author's profile picture Akshay Mohite on Rails |

Rails 5.1 Encrypted secrets management

Before Rails 5.1, secrets management was a hassle. Gems like dotenv-rails, sekrets were being used to manage secrets in Rails applications. Rails 5.1 released secrets management feature based on sekrets gem. This article will explain how to manage application secrets with Rails 5.1 applications.

»
Author's profile picture Akshay Mohite on Rails |

Rails ActiveRecord Subqueries

Writing subqueries can be a tedious job, when working with Rails ActiveRecord. Rails ActiveRecord can handle subqueries. Subqueries can be written by passing ActiveRecord Relation in where clause. This article discusses how to write subqueries with ActiveRecord in Rails.

»
Author's profile picture Akshay Mohite on Rails |

Rails find missing indexes on tables with lol_dba gem

Missing indexes on database tables causes performance issues. lol_dba gem helps finding out missing indexes on database table in Rails code. lol_dba performs static analysis of the code to find out missing indexes. We will take a look at how to use lol_dba to find out indexes. lol_dba also helps creating a migration to add the requred indices.

»
Author's profile picture Akshay Mohite on Rails |

Rails Soft Delete with Paranoia gem

Sometimes, we need to delete records logically but not physically. Paranoia gem allows us to soft delete records. Records are marked as deleted by a column in a table. Article helps understand how to soft delete Records in Rails with Paranoia gem.

»
Author's profile picture Akshay Mohite on Rails |

Rails ActiveAdmin N+1 query optmization for controller actions

ActiveAdmin is one of the most used Admin interface with the Rails applications. It provides an easy way to view, create, update or destroy records. When a model is registered with ActiveAdmin, it may have associations. In that case, when index page is opened for the model, it fires N+1 queries to fetch the values of an associated class. This presents performance bottleneck. This article will help remove N+1 querying on ActiveAdmin controller index action for a model.

»
Author's profile picture Akshay Mohite on Rails |

Rails form_with - alternative to form_for and form_tag

Rails 5.1 added form_with form helper method that provides capabilities of form_for and form_tag. Rails unified form_for and form_tag that provide similar interfaces to generate forms with form_with helper.

»
Author's profile picture Akshay Mohite on Ruby |

Safe navigation operator in Ruby

Ruby 2.3 has introduced safe navigation operator. Safe navigation operator returns nil if a method is called on a nil object. Before this, if a method is called on a nil object, it throws an error as given below.

»
Author's profile picture Akshay Mohite on Ruby |

Use find_each instead of all.each in Rails

In Rails, sometimes we need to iterate over all the records from a model. To achieve this people prefer to use all.each on a model. This can lead to usage of memory if there are millions (huge number of) records in the table.

»
Author's profile picture Akshay Mohite on Rails |

Array comparison in ruby

I was working on a problem to check on a large array of integers to check whether it is a part of another array or not. Both the arrays have unique elements, but not in sorted order. So there are multiple ways possible to this, but I was not sure that which approach is most efficient. I searched for this but couldn’t come across anything valuable. Then I decided to benchmark whole approach by my self.

»
Author's profile picture Gaurav Singh on Ruby |

Ruby do not use keys method on a hash in loop

Today, I was looking at the internal working of a gem. While working on the same, I came across the code where the network calls were being made and it had to check if the response params has some set of keys.

»
Author's profile picture Akshay Mohite on Ruby |

Google Cloud HTTPs load balancing with Letsencrypt certificate

To setup HTTPs load balancer on Google Cloud Platform , we need to create an SSL certificate beforehand. Letsencrypt can be used to create an SSL certificate free of cost. The SSL certificate provided by Letsencrypt is valid for 3 months. In this article, we will learn how to setup HTTPs load balancing using Letsencrypt certificate for a Ruby on Rails application on Google Cloud Platform.

»
Author's profile picture Akshay Mohite on Devops |

Asynchronous Programming in Ruby

In traditional programming practice, I/O operations happen synchronously. The main thread will be blocked until I/O operation gets completed and CPU remains idle for the time I/O operation is getting completed. Consider the following example where CPU is blocked until query execution completed.

»
Author's profile picture Gaurav Singh on Ruby |