Posts

Showing posts from April, 2020

Making complex queries readable with the ActiveRecord

Image
Making complex queries readable with the ActiveRecord Sometimes, we need multiple tables to join in our project and these queries almost not be readable. The first thing that comes to our mind is of course Gems in Ruby on Rails. Today I will focus on how we can make complex queries more readable without using any library or Gem. I have some models in our current project on Ruby On Rails 5.2 : models/courier.rb class Courier < ApplicationRecord has_one :main_contract, :as => :customer has_many :customer_contracts #default pagination paginates_per 20 end models/main_contract.rb class MainContract < ApplicationRecord belongs_to :customer, polymorphic: true, optional: true belongs_to :product end models/customer_contract.rb class CustomerContract < ApplicationRecord belongs_to :product belongs_to :airline, optional: true belongs_to :airport, optional: true belongs_to :courier, optional: true end models/airport