Hi there 馃憢

Welcome to my blog, My name is Ashish Gaur

Single Table Inheritance

What is Single Table Inheritance ? Single Table Inheritance is a mechanism through which ActiveRecord can share fields and behavior between different models. For eg. let鈥檚 say we need to create a model vehicle and car. Since vehicle and car will share their attributes and methods we can use Single Table Inheritance on them. Let鈥檚 generate vehicles model and a cars model with vehicles as its parent: rails generate model vehicle type:string color:string price:decimal{10....

April 12, 2024 路 2 min 路 Ashish Gaur

Polymorphic Associations

What is Polymorphic Associations ? With Polymorphic Association a model can belong to more than one models with a single association. Let鈥檚 take an example, we鈥檒l use polymorph associations on the following schema: In the above schema picture belongs to both employee and products. Creating picture model with polymorphic association will look like this: class Employee < ApplicationRecord has_many :pictures, as: :imageable end class Product < ApplicationRecord has_many :pictures, as: :imageable end class Picture < ApplicationRecord belongs_to :product, as: :imageable belongs_to :employee, as: :imageable end Now we can use Picture model with Product and Employee:...

April 12, 2024 路 1 min 路 Ashish Gaur

Eager Loading

What is Eager Loading ? Eager loading is a mechanism through which ActiveRecord loads the associated records of an ActiveRecord object in memory, reducing the number of executed sql queries. There are 3 ways to do eager loading in ActiveRecord: includes preload eager_load Why do we need Eager Loading ? Let鈥檚 say we鈥檝e to get the author鈥檚 last name of some books. For that the query will look like:...

April 11, 2024 路 3 min 路 Ashish Gaur

Strategy Pattern

Strategy pattern is a behavioral pattern which is used when there is a need to choose an algorithm on runtime. It鈥檚 a very good example of open/closed principle. What is Open/Closed Principle Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. The idea is to add new functionality without changging the existing classes. So how does Strategy Pattern makes sure open/closed principle is used in our code, lets find out....

October 14, 2023 路 3 min 路 Ashish Gaur

Builder Pattern

What is Builder pattern ? The builder pattern is a creational design pattern used for building complex objects involving several steps to create or require multiple sub objects. The pattern helps in abstracting away the process of instantiating the sub objects and the relationship among them. Builder pattern helps in: Allowing to create different representation of a complex object. Simplifying creating an object with several steps or instantiation of sub objects....

October 8, 2023 路 4 min 路 Ashish Gaur