Module: Bulletproof::Detectors

Defined in:
lib/bulletproof/detectors/excessive_includes_detector.rb

Defined Under Namespace

Classes: ExcessiveIncludesDetector

Constant Summary collapse

LIMITING_METHODS =

Rubyソースを静的解析し、メモリ過剰消費につながるincludes呼び出しを検出する

検出の前提:

includesのネスト深さ・アソシエーション数が大きくても、
件数を絞るメソッドがチェーンにあれば安全とみなす。

危険パターン(フラグを立てる):

User.includes(posts: { comments: :author }).all          # 全件 + 深いネスト
User.includes(:a, :b, :c, :d)                           # 全件 + 多アソシエーション

安全パターン(フラグを立てない):

User.includes(posts: { comments: :author }).limit(10)    # 件数制限あり
User.includes(posts: { comments: :author }).find(1)      # 単件取得
User.includes(posts: { comments: :author }).first        # 単件取得
User.includes(:a, :b, :c, :d).page(1).per(20)           # ページネーションあり
%i[
  limit find find_by find_by!
  first first! last last! take take!
  page paginate per per_page
].freeze