ruby

Drawbacks of Active Record

Active Record is a pattern used in software engineering that represents the database in an object-oriented manner. In other words, it’s a way to use objects to interact with a database instead of writing SQL queries directly. In Ruby on Rails, Active Record is the default ORM (Object-Relational Mapping) that allows developers to define database…

ruby

Ruby on Rails object lifecycle and callbacks

In ROR, the lifecycle of an object is attached to the database of the application. Objects are created in memory first then later stored in the database. An object have following lifecycle Creation Validation Save Update Deletion These lifecycle have callbacks attached to each of them by which we can inject our custom logic. Creation…

ruby

Functions in Ruby that make life easy.

I have been using ROR for the last eight months and started loving this language because of the poetic way it allows me to write the code. In this journey, I have found some functions which helped me to keep the code short and improved readability significantly. 1. blank? and present? A common use case…

ruby

Writing my first monkey patch in rails

The motivation From last few months we are facing an error which have become hard to replicate in development environment. The error is This error occurs rarely and replicating have become hard. The idea was to log the query and see why the query is forming correctly. Then I used, query.to_sql to log the query…

ruby

Adding Caching Helper in Rails

When a particular API processing slow or performing a heavy calculation or have heavy DB queries. One of the solution to optimize the API that comes to our mind is to cache the response for appropriate amount of time. This will help us to improve the performance by overcoming the mentioned issues. It is also…

ruby

Reload in ruby and when to use it.

Consider we have a model student when we access data from this using the following command. The variable student contains the data of a student. Each student has enrolled in many courses, which is another model. On printing the student, we get the following data Consider if I update the course model, The expected behavior…

ruby

Send keyword in Ruby

Recently I have been introduced to ruby, while working on the project I saw this function is been used many times in our existing codebase. Let’s discuss what problem it solves. Consider you want to call a list of functions dynamically, eg. Consider an interaction to list animals data which takes the animal name as…