Help with a simple search form

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 %>

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 %>

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. [email protected]

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