# Search with more then one argument

**URL:** <https://www.ruby-forum.com/t/search-with-more-then-one-argument/123794>\
**Category:** Rails\
**Created:** [December 20, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/search-with-more-then-one-argument/123794 "2007-12-20T17:53:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [December 20, 2007, 5:53pm UTC](https://www.ruby-forum.com/t/search-with-more-then-one-argument/123794/1 "2007-12-20T17:53:37Z")

</div>

I’am trying to create a search sytem with two arguments  
When one argument is choosen it needs find all the objects with that  
argument  
and when two choosen it needs to find all objects with both arguments.

In my view

\<% form\_tag :action =\> ‘tuinen’, :method =\> ‘get’ do %\>  
\<%= options = [[“Selecteer een provincie”, “”]] + User::PROVINCES  
select(“search”, nil, options)  
%\>  
\<%= select ‘heading’, nil, Heading.find(:all).collect {|p| [ p.name,  
p.id ] }, { :prompt =\> “selecteer een rubriek”} %\>  
\<%= submit\_tag “Zoek mijn tuin”%\>  
  
  
\<% end %\>

the problem is into my controller i can’t find no way to seperate the  
aruments  
i tried several methods but nothing works

my basic method is like this

if @params[:search]  
@gardens = Garden.find(:all, :conditions =\> {:provincie =\>  
@params[:search], :heading\_id =\> @params[:heading], :destroyed =\> 0})

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [December 21, 2007, 1:12am UTC](https://www.ruby-forum.com/t/search-with-more-then-one-argument/123794/2 "2007-12-21T01:12:19Z")

</div>

I think you might want something like

@gardens = Garden.find(:all, :conditions =\> [“provincie = ? and  
heading\_id = ? and destroyed = 0”, @params[:search],  
@params[:heading] ]

This variant of :conditions creates a SQL fragment for a where clause  
that replaces the ?'s in order with the values provided after the  
first comma (and has the added benefit of preventing SQL injection).

Is the what you were looking for?

Tom

On Dec 20, 11:53 am, GA Gorter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [December 21, 2007, 1:14am UTC](https://www.ruby-forum.com/t/search-with-more-then-one-argument/123794/3 "2007-12-21T01:14:05Z")

</div>

Whoops – I missed a closing ) after the last ]

🙂

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [December 21, 2007, 9:32am UTC](https://www.ruby-forum.com/t/search-with-more-then-one-argument/123794/4 "2007-12-21T09:32:57Z")

</div>

It does not what i want

I want a system when i choose only a province i get all gardens in that  
specific province  
And when i set the heading i get all gardens with that specific heading  
And when i set them both i get all gardens in that province and that  
heading

In this way only when i set both i get the right results even like the  
system i had in the post above.
