rails ActiveRecordでassociation先のデータがない場合には取得しないようにする

本稿について

本稿はサイト運営者が学んだことなどを記したメモの内容です。
全てが正確な情報とは限りません。ご注意ください。また修正するべき点は適時修正していきます
地味に知らなかったのでメモ
scopeやデータをassociationで引く場合に、連携先のデータがない場合にはselectしない様にする方法

class ParentContent < ApplicationRecord
  belongs_to :content, -> { order(published_at: :desc) }

  scope :list, -> (prent_id, page) {
  includes(:content).where.not(contents: { id: nil })
  }  



Back