Posts

Showing posts from March, 2023

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

Ruby 3 Type profiling: Dynamic data type at run time in ruby

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... Ruby 3 introduces type profiling, a new tool that lets us examine how programmes behave at runtime and determine the types of objects there. By helping the Ruby interpreter to produce more effective code, this can help performance. Type profiling is an optional feature that can be turned on or off at runtime. This blog post will go through the new type profiling functionality in Ruby 3 and give a practical use for it. To enable type profiling, we need to pass the -v command-line option to the Ruby interpreter. Here's an example: In command A, we enable type profiling and the just-in-time (JIT) compiler, which can improve performance by compiling Ruby code into machine code at runtime Once ty

Rails 3 Ractor : a parallel execution feature of Ruby without thread-safety concerns

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... Ruby 3 introduces Ractors, commonly referred to as Ruby Actors, as a new concurrency feature. They enable us to execute several threads of code concurrently without having to share memory or state. As a result, concurrent and parallel programming is made simpler, which can enhance performance and scalability. It's necessary to grasp the fundamentals of concurrency and parallelism in programming before we delve into the specifics of Ractors. Concurrency is the capacity of a programme to manage many activities or execution threads concurrently. The term "parallelism" describes a program's capacity to run numerous processes or threads concurrently while utilising multiple processors o

Ruby 3 : New features

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 most recent significant version of the Ruby programming language is Ruby 3. It has a number of interesting new features and advancements and was published on December 25, 2020. We will examine some of Ruby 3's updated features in this blog. -> Ractors Ruby 3 introduces Ractors, also referred to as Ruby Actors. They enable us to execute several threads of code concurrently without having to share memory or state. As a result, concurrent and parallel programming is made simpler, which can enhance performance and scalability. Ractors are simple to use and integrate with existing code because they are designed on top of the Thread API that already exists. -> Type Profiling Type profiling,