Tuesday 4 August 2020

Avoid N+1 Queries

1 . Location.includes(:users).where(users: {username: "ashish"}) 
Lead.includes(:contacts).where("contacts.primary" =>true)

2. Person.includes(notes: [:grades]).where(notes: {important: true, grades: {important: true})

3. 
  class Person < ActiveRecord::Base
has_many :notes
has_many :important_notes, -> { where(important: true) }, class_name: "Note"
end

Person.preload(:important_notes)
4. Person.eager_load(:notes)

No comments:

Post a Comment

Revert last commit or second last and more....

 Git revert commit_id -m 1 this command willl revert last commit  Git revert commit_id -m 2 this command will revert second commit with same...