Microservices in Rails

Microservices architecture is an architectural style that structures an application as a collection of distributed services. This allows better separation of responsibilities, greater flexibility in the choice of technologies for each service, and easier scalability and fault tolerance. In simple terms in microservices architecture services have their own repository, dedicated cpu/memory, and may use different technologies and langauges depending on their usecase. While in monolithic architecture the services share the same codebase and cpu/memory....

August 17, 2024 · 5 min · Ashish Gaur

Shallow vs Deep Nested Resources in Rails

In Rails, in order to create routes we’ve define resources. Resources provide a mapping between an http verb with its URL and a controller action. For eg. to create routes for posts we’ve to add following to our routes.rb: # routes.rb Rails.application.routes.draw do ... resources :posts end This will generate the following seven routes each of it maps to PostsController and one of its action HTTP Verb Path Controller#Action Used for GET /posts posts#index display a list of all posts GET /posts/new posts#new return an HTML form for creating a new post POST /posts posts#create create a new post GET /posts/:id posts#show display a specific post GET /posts/:id/edit posts#edit return an HTML form for editing a post PATCH/PUT /posts/:id posts#update update a specific post DELETE /posts/:id posts#destroy delete a specific post What are Nested Resources ?...

August 16, 2024 · 3 min · Ashish Gaur

Setting Up Devise With Rails 7

In this blog we’re going to look at how to setup devise gem with rails 7 using importmaps. If you want to just setup devise without knowing the nitty gritty follow tldr; otherwise scroll past it for the full blog. tldr; Follow thsese steps to setup devise with rails. You must have rails 7.x installed on your system. Set up new app rails new devise-tutorial Inside the devise-tutorial folder create a new controller rails g controller Home index Add devise gem to your Gemfile bundle add devise Install devise configuration into your Rails app bundle exec rails g devise:install Generate devise views rails g devise:views Generate a user model for devise bundle exec rails g devise user Apply rails migration bundle exec rails db:migrate Restart the rails server if already running or start the rails app using rail s Go to http://localhost:3000/users/sign_in THE END What is devise ?...

June 6, 2024 · 7 min · 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 while using a single table. For eg. let’s say we need to create a Vehicle model and Car model. Since Vehicle and Car will have common attributes and methods we can use Single Table Inheritance on them. The schema of our example will look like this:...

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’s take an example, we’ll use polymorphic associations as shown in the following schema: In the above schema pictures belongs_to both employees and products. Adding the polymorphic association in the Picture model 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 · 2 min · Ashish Gaur