# Help with a simple search form

**URL:** <https://www.ruby-forum.com/t/help-with-a-simple-search-form/109824>\
**Category:** Rails\
**Created:** [July 19, 2007, 12:04pm UTC](https://www.ruby-forum.com/t/help-with-a-simple-search-form/109824 "2007-07-19T12:04:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Acroterra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Acroterra](https://www.ruby-forum.com/u/Acroterra)\
**Post date:** [July 19, 2007, 12:04pm UTC](https://www.ruby-forum.com/t/help-with-a-simple-search-form/109824/1 "2007-07-19T12:04:12Z")

</div>

Hi, I am working on a project that needs a simple search feature but I  
have hit a roadblock. I need to design a search form that searches a  
lodging database for certain criteria such as lodge town and lodge  
amenities but my solution only partially works. My problem is with the  
Lodge.find method where I keep getting ActiveRecord::StatementInvalid  
errors. Because I am using a join table for the lodge amenities the  
find method can’t find a has\_and\_belongs\_to\_many relationship. I  
thought active record would be able to handle this relationship for  
me. Any help would be greatly appreciated. Here is my code:

## in lodges\_controller:

def search  
@towns = Town.find(:all, :order =\> “name”)  
@seasons = Season.find(:all)  
town\_id = params[:town\_id]  
seasons = :season\_list

```
@results = Lodge.find(:all, :conditions => ["town_id = ? and

```

seasons = ?", town\_id, seasons])  
end

## in search.rhtml

\<% form\_tag( :action =\> :search ) do %\>

**Search by Town:** \<%= select( ‘town\_id’,  
params[:town\_id], @towns.collect{|t| [t.name, t.id]} ) %\>

  

**Search by Season:** \<br \>  
\<% for i in @seasons -%\>  
\<%= check\_box\_tag(‘seasons\_list[]’, params[i.id], checked = true)  
%\> \<%= i.season %\>   
  
\<% end -%\>

\<%= submit\_tag( "Search" ) %\>

\<% end %\>

---

<div class="post-metadata">

**Author:** ![Acroterra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Acroterra](https://www.ruby-forum.com/u/Acroterra)\
**Post date:** [July 19, 2007, 2:15pm UTC](https://www.ruby-forum.com/t/help-with-a-simple-search-form/109824/2 "2007-07-19T14:15:43Z")

</div>

This might be a little better (warning, untested code ahead):

## in lodges\_controller:

def search  
@towns = Town.find(:all, :order =\> “name”)  
@seasons = Season.find(:all)  
@results = Lodge.find(:all, :conditions =\> { :town\_id =\>  
params[:town\_id],  
:seasons  
=\> params[:seasons\_list] } )  
end

If my memory is correct, this means you will be able to pass nill,  
single values of arrays to each of the search values.  
You could then change your search form to include check boxes for the  
towns.

Andrew

Acroterra wrote:

> Hi, I am working on a project that needs a simple search feature but I  
> have hit a roadblock. I need to design a search form that searches a  
> lodging database for certain criteria such as lodge town and lodge  
> amenities but my solution only partially works. My problem is with the  
> Lodge.find method where I keep getting ActiveRecord::StatementInvalid  
> errors. Because I am using a join table for the lodge amenities the  
> find method can’t find a has\_and\_belongs\_to\_many relationship. I  
> thought active record would be able to handle this relationship for  
> me. Any help would be greatly appreciated. Here is my code:
> 
> ## in lodges\_controller:
> 
> def search  
> @towns = Town.find(:all, :order =\> “name”)  
> @seasons = Season.find(:all)  
> town\_id = params[:town\_id]  
> seasons = :season\_list
> 
> ```
> @results = Lodge.find(:all, :conditions => ["town_id = ? and
> 
> ```
> 
> seasons = ?", town\_id, seasons])  
> end
> 
> ## in search.rhtml
> 
> \<% form\_tag( :action =\> :search ) do %\>
> 
> **Search by Town:** \<%= select( ‘town\_id’,  
> params[:town\_id], @towns.collect{|t| [t.name, t.id]} ) %\>
> 
>   
> 
> **Search by Season:** \<br \>  
> \<% for i in @seasons -%\>  
> \<%= check\_box\_tag(‘seasons\_list[]’, params[i.id], checked = true)  
> %\> \<%= i.season %\>   
>   
> \<% end -%\>
> 
> \<%= submit\_tag( "Search" ) %\>
> 
> \<% end %\>

---

<div class="post-metadata">

**Author:** ![Acroterra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Acroterra](https://www.ruby-forum.com/u/Acroterra)\
**Post date:** [July 19, 2007, 6:17pm UTC](https://www.ruby-forum.com/t/help-with-a-simple-search-form/109824/3 "2007-07-19T18:17:13Z")

</div>

Using your code I am getting a NoMethodError - "undefined method for  
{:seasons=\>:seasons\_list, :town\_id=\>nil}:hash

Any other suggestions? Thanks for you help.  
Wes

On Jul 19, 6:15 am, Andrew S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Acroterra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@Acroterra](https://www.ruby-forum.com/u/Acroterra)\
**Post date:** [July 20, 2007, 12:02pm UTC](https://www.ruby-forum.com/t/help-with-a-simple-search-form/109824/4 "2007-07-20T12:02:26Z")

</div>

Which version of rails are you running?  
The ability to use a hash for conditions was added in 1.2 (I think)
