TODO N+1
Introduction
Example
# Post Model
class Post < ActiveRecord::Base
belongs_to :author
end
# Author Model
class Author < ActiveRecord::Base
has_many :posts
end# Controller
class PostsController < ApplicationController
def index
@posts = Post.order(created_at: :desc)
end
endCase 1: Default - lazy loading
Case 2.1: preload - eager loading / separated queries
preload - eager loading / separated queriesCase 2.2: eager_load - eager loading / one query
eager_load - eager loading / one queryCase 2.3 includes - smarter
includes - smarterLast updated