0
Help us understand the problem. What are the problem?

posted at

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

これは「「はじめに」の 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

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
0
Help us understand the problem. What are the problem?