LoginSignup
0
0

More than 1 year has passed since last update.

[6行メモ] ActiveRecordのeagar_load後のwhere条件をControllerのParam値で動的に変更したい時

Posted at

これは「「はじめに」の Advent Calendar 2021」3日目の記事です。

Active Record 初心者が今日学んだこと。

result = Foo.where(name: params[:name])
            .eager_load(:bars)
if params[:bar_name].present?
  result.where(bars: { name: params[:bar_name] })
end

render :json => result

※もっと良い方法があったら教えて下さい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0