1
条件付きでクエリメソッドをActiveRecord :: Relationに追加する方法はありますか?ActiveRecord関係:クエリメソッドの追加
たとえば、私はユーザーを検索していますが、特定の条件下で検索に含まれたlast_nameのみでした。 ActiveRecord :: Relationオブジェクトにクエリを追加できますか?
i_want_to_search_for_last_names = true
pending_relation = User.where(:first_name => "John")
pending_relation << where(:last_name => "Doe") if i_want_to_search_for_last_names
@users = pending_relation.all
これは機能します!これは複数行の 'User.where(:first_name =>" John ")です。ここで(:last_name =>" Doe ")'です。あなたのアプローチは私に起こっているはずです:)脳の衝撃に感謝します。 – dhulihan
あなたは歓迎です、私はいつも助けてうれしいです:) – bor1s