Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check Presence of multiple params

I need to check the presence of multiple params. Currently what i have written is

if params[:p1].present? && params[:p2].present? && params[:p3].present?
  # Do something
end

Is there a more efficient way to do this?

like image 709
prajeesh Avatar asked Jan 13 '16 12:01

prajeesh


1 Answers

You can use the Enumerable.all? method:

%i( p1 p2 p3 ).all? { |key| params[key].present? }

Another alternative, if you need the values, it to fetch them and check the presence.

params.values_at(*%i( p1 p2 p3 )).all?(&:present?)

or

params.values_at(:p1, :p2, :p3).all?(&:present?)
like image 106
Simone Carletti Avatar answered Nov 16 '22 04:11

Simone Carletti