Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i simplify conditions when i have much params in my search?
    text
    copied!<p>For example</p> <p>I have 5 params in my search view:</p> <pre><code> params[:a] params[:b] params[:c] params[:d] params[:e] </code></pre> <p>And I want to search in my controller when:</p> <pre><code> params[:a].present? and params[:b].nil? params[:a].present? and params[:c].nil? params[:a].present? and params[:e].nil? params[:b].present? and params[:c].nil? params[:b].present? and params[:d].nil? ............ params[:a].nil? and params[:b].nil? ............. </code></pre> <p>Here is what I tried:</p> <pre><code> controller: if params[:a].nil? @query else if params[b].nil? @another_query else ...........#and it will continue with lots of conditions according with params end end </code></pre> <p>Well there are lots of conditions that I can try</p> <p>Here is using a real example I got this error "undefined method `paginate' for nil:NilClass"</p> <pre><code> #table policies id num_policy type_id doc name 1 2323 1 181844 Charles 2 2121 2 151511 Aguardientico 3 3423 2 434344 Benjamin #controller if params[:num_policy].present? @search= Policy.find(:all,:conditions=&gt;['num_policy LIKE ?',params[:num_policy] ]) end if params[:type_id].present? @search= Policy.find(:all,:conditions=&gt;['type_id LIKE ?',params[:type_id] ]) end if params[:doc].present? @search= Policy.find(:all,:conditions=&gt;['doc LIKE ?',params[:doc] ]) end if params[:name].present? @search= Policy.find(:all,:conditions=&gt;['name LIKE ?',params[:name] ]) end if params[:doc].present? and params[:type_id].present? @search= Policy.find(:all,:conditions=&gt;['doc LIKE ? and type_id LIKE ?',params[:doc],params[:type_id] ]) end @policies= @search.paginate( :page =&gt; params[:page], :per_page =&gt;2) #view &lt;% form_tag :controller=&gt;"policy",:action=&gt;"search" do %&gt; &lt;%= text_field_tag "num_policy",params[:num_policy] %&gt; &lt;%= text_field_tag "doc",params[:doc] % &lt;%= text_field_tag "name",params[:name] % &lt;%= text_field_tag "type_id",params[:type_id] % &lt;% end %&gt; &lt;% @policies.each do |p| %&gt; &lt;%= p.num_policy %&gt; &lt;%= p.type_id %&gt; &lt;%= p.doc %&gt; &lt;%= p.name %&gt; &lt;% end %&gt; </code></pre> <p>But if remove paginate I got this error "You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each"</p> <pre><code> #controller @policies= @search #says that my line &lt;% @policies.each do |p| %&gt; is nil </code></pre> <p>Is there any way to control them?</p> <p>Please i just want opinions maybe someone happened it too..</p> <p>Thanks.</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