Note that there are some explanatory texts on larger screens.

plurals
  1. PORails will_paginate using :conditions, only if param present in URL
    text
    copied!<p>i'm trying to achieve paginated list of records from database, with conditions given in url.</p> <p>for example: if url is like: <code>?name=Michael&amp;age=20</code> it should display all Michaels with age 20 etc.</p> <p>But if url is like <code>&amp;age=20</code>, it should display all people with age 20 and ignore the "name" parameter.</p> <p>I'm paginating 2 lists on 1 page, both come from same model, only vary by position, so i've got 2 paginations defined in my controller:</p> <pre><code>@boss = Person.paginate(:page =&gt; params[:boss_page], :conditions =&gt; "position = 1") @manager = Person.paginate(:page =&gt; params[:manager_page], :conditons =&gt; "position = 2") </code></pre> <p>How to now add conditions from URL only if param is present in it?</p> <p>I was trying this way:</p> <pre><code>conditions = [] conditions &lt;&lt; [ "age = ?", params[:age] ] if params[:age].present? </code></pre> <p>but it gives me error when I pass param in URL:</p> <pre><code>undefined method `%' for ["age = ? ", "1"]:Array </code></pre> <p>The next question is how to merge conditions from this array with condition "position = 1"?</p> <p>UPDATE: I understand how to do pagination with conditions, I just don't get what is best idea to specify conditions based on GET parameters. I could always do it like this:</p> <pre><code>if params[:age].present? @boss = version with age conditons else if params[:age].present? and params[:name].present? @boss = version with age and name else if params[:name].preset? @boss = version with name only </code></pre> <p>but I believe that in rails it can be done simpler.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload