Posts

Rails Sync Postgresql and Elasticsearch Database of a Model

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about how can we keep Postgresql (a SQLDB) and Elasticsearch (a nonSQL DB) with the help of a background job processor and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... Most of the time due to increase in DB record size we try to split our DB into two DB based on the requirement like one is Transaction oriented (SQL DB) and another Search oriented (Non SQL DB) with limited data. In my case we have Postgresql for storing all the records and used for entire business operation and for Search support we have elasticsearch with minimum indexes (tables) and their entire documents. For better understanding lets consider in PG DB we have Employee table with id, name, dob, address etc and some more other tables, whereas in Elasticsearch we have only Employee index (table) with all its documents dupli

Rails Postgresql Query timeout set and skip during migration

Hi, I am  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the rails DB (Postgresql) query time out setting and how can we avoid this timeout setting during migration and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... Question: Rails and postgresql I want to set query timeout to 10 sec but skip to check this timeout setting during rails migration how can i achieve? Answer: To set a query timeout of 10 seconds for PostgreSQL in a Rails application, you can use the statement_timeout configuration option in your database.yml file. Here is an example configuration that sets the query timeout to 10 seconds: This will set the query timeout to 10 seconds for all queries executed by the PostgreSQL adapter in the production environment. To skip checking the query timeout setting during Rails migrations, you can use a custom connection adapter tha

Optimizing Database Queries in Ruby on Rails: Best Practices and Examples - Part 2

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... Before jumping over different code snippets lets read some theory first,here are some tips for optimizing Ruby on Rails ActiveRecord queries: Use eager loading: As discussed earlier, eager loading can be used to fetch associated records in a single query, rather than making a separate query for each association. This can greatly reduce the number of database queries and improve performance. Avoid N+1 queries: N+1 queries occur when a query is made to fetch a record, and then additional queries are made for each associated record. This can be avoided by using eager loading, or by preloading associated records using the includes method. Use appropriate indexing: Indexing can greatly im

Ruby on Rails: Understanding, Optimizing, and Mastering Database Queries with Preloading, Eager Loading, Includes, and Joins

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... In this article we will explore best practices to avoid N+1 query issue in ruby on rails and also best practices to make database query optimised. And we also need to keep below stuffs in mind while doing the code review of our peer. Ruby on Rails offers a number of methods for preloading related items in order to get around the N+1 query problem. When the application does his N+1 database queries, the N+1 query problem arises. where N represents the total number of records found. Serious performance problems may result from this, especially when working with huge datasets. Using eager loading, which preloads related objects when the database is requested, is one way to deal with this issue. An inc

Ruby Array basic and advance methods

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the ruby array's some basic and advance methods and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... Ruby Array basic methods push - adds an element to the end of the array pop - removes the last element from the array and returns it shift - removes the first element from the array and returns it unshift - adds an element to the beginning of the array less length - returns the number of elements in the array join - converts the array into a string, with an optional separator include? - returns true if the array contains a specified element reverse - returns a new array with the elements in reverse order sort - returns a new array with the elements sorted in ascending order u

Memory management and optimisation in Ruby on Rails

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... The process of allocating and releasing memory for variables and objects in a computer language is known as memory management. The garbage collector, which automatically releases memory that is no longer needed by programme objects, manages memory in the Ruby programming language. The mark-and-sweep algorithm is used by Ruby's garbage collector to decide which memory objects are still in use and which ones can be released. The method searches through every memory object and flags it as "live" if the programme is currently using it. If an object is not designated as "live," it is then treated as garbage and released from memory. Because inefficient memory utilisation can caus

Ruby on Rails configure CORS setting with rack cors gem

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the CORS concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... What is CORS? Cross-Origin Resource Sharing, sometimes known as CORS, is a security feature built into web browsers that enables web servers to manage which domains can access their resources. It is used to stop rogue websites or programmes from gaining unauthorised access to a web server's resources. In plainer terms, a web browser will submit a CORS request to the server whenever a web application makes a request to a domain other than the one it is currently executing on in order to determine whether it is authorised to access the resources of that domain. The browser can retrieve the resources and show them to the user if the server agrees to the request. Why CORS is important? If CORS is not used, malici

Ruby on Rails Optimisation and best practices

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... Achieving Lightning-Fast Ruby on Rails Performance: Tips and Tricks for Optimizing Your Application Ruby on Rails is a popular web development framework known for its simplicity and ease of use. However, as your application grows and scales, you may start to encounter performance issues that can impact user experience and overall system efficiency. In this post, we'll explore some of the most effective strategies for optimizing your Ruby on Rails application to achieve lightning-fast performance. 1. Caching: One of the most effective ways to improve Ruby on Rails performance is to use caching. Caching allows you to store frequently accessed data in memory, reducing the number of database queri

Exceptions in Ruby and its types

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... Popular programming language Ruby is renowned for its ease of use and adaptability. Unfortunately, Ruby is susceptible to mistakes that can happen while executing code, just like any other programming language. These mistakes are known as exceptions, and Ruby includes a sophisticated method for dealing with them. We'll go through exceptions in this blog post, including what they are, how Ruby handles them, and the many kinds of exceptions you might run against. What are exceptions in Ruby? A programming error is an exception. It takes place while the programme is running. When an exception occurs, Ruby leaps to a predetermined section of code known as a "rescue" block, interrupting

Ruby best secure code approaches

Hi, This is  Shubham Mishra  from India,  this is the part of  Ruby on Rails  exploration journey.  In this post we will discuss about the ruby's some special concepts and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started... It's crucial for software engineers to build secure code in addition to functional code. In order to ensure the security of our apps, we will discuss several security best practises in this blog. 1. Avoid SQL Injection One of the most common security vulnerabilities in web applications is SQL injection. To avoid SQL injection in Ruby, use the built-in parameter binding mechanism provided by your database driver. This ensures that user input is properly escaped and prevents malicious SQL statements from being executed. In above snippet rather than block A, use block B approach, This way, the name parameter is properly escaped and cannot be used to inject malicious SQL code. 2. Validate User I