title: How do I create a variable in the controller for single or multiple params
body: <pre><code>def index
    @users = User.all.paginate(page: params[:page])
    @users = User.named(params[:name]).paginate(page: params[:page]) if params[:name].present?
    @users = User.countryname(params[:country]).paginate(page: params[:page]) if params[:country].present?
    @users = User.gender(params[:gender_type]).paginate(page: params[:page]) if params[:gender_type].present? 
  end
  </code></pre>
  
  <p>The following code works fine if only <code>:name</code> or <code>:country</code> or <code>:gender_type</code> is <code>present</code>. But it does not work if multiple params are present. What is the DRY way of writing this code for multiple params? Obviously, I do not want to create a different line of code for each possible combination of params. </p>
  
  <p>Here are the scopes: </p>
  
  <pre><code>class User
  scope :countryname, -&gt; (country) { where("country ILIKE ?", "%#{country}%")}
  scope :gender, -&gt; (gender_type) { where gender_type: gender_type}
  scope :named, -&gt; (name) { where("name ILIKE ?", "%#{name}%")}
  </code></pre>
  
  <p>If I have a query string of </p>
  
  <blockquote>
    <p>example.com/users?name=sam&amp;gender_type=male </p>
  </blockquote>
  
  <p>it simply returns all users with names like sam and ignores their gender... I would need to code:</p>
  
  <pre><code>@users = User.gender(params[:gender_type]).named(params[:name]).paginate(page: params[:page]) if params[:gender_type] &amp;&amp; params[:name].present? 
  </code></pre>
  
  <p>but I do not want to have to write a new line of code for every single combination of parameters. </p>