# Query question

**URL:** <https://www.ruby-forum.com/t/query-question/132903>\
**Category:** Rails\
**Created:** [March 25, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/query-question/132903 "2008-03-25T18:02:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shawn\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/977dab/32.png) [@Shawn\_B](https://www.ruby-forum.com/u/Shawn_B)\
**Post date:** [March 25, 2008, 6:02pm UTC](https://www.ruby-forum.com/t/query-question/132903/1 "2008-03-25T18:02:00Z")

</div>

How can I do the following

I have a for as follows  
\<% form\_tag companies\_path, :method =\> ‘get’ do %\>

 Province: \<%= hidden\_field\_tag :test, "test" %\> \<%= select\_tag "province", " Prince Edward Island New Brunswick Nova Scotia Newfoundland" %\> Product Category: 

| \<% end %\>

I want to dynamically build the SQL statement to look something like  
this  
SELECT \* FROM `companies` WHERE (0=0 and province = Prince Edward  
Island and (pre\_fabricated = 1or wood\_based\_panels = 1))  
The values in the where statement can change based on what is selected  
in the form.

My Model  
if search[:test]  
#province = search.delete(:province)  
test = search.delete(:test)  
action = search.delete(:action)  
controller = search.delete(:controller)  
sql = “0=0”  
search.each do |key, val|  
if[:province]  
sql += " and #{key} = #{val}"  
else  
sql += “and (#{key} = #{val} or”  
end  
sql += “)”  
end  
find(:all, :conditions =\> [sql])  
else  
find(:all)  
end  
end

This is not working Please help!!

| \<%=check\_box\_tag :wood\_based\_panels, 1, params[:wood\_based\_panels] %\> Wood based panels |
| \<%=check\_box\_tag :pre\_fabricated, 1, params[:pre\_fabricated] %\> Complete pre-fabricated building and houses |
| \<%=check\_box\_tag :wood\_cabinets, 1, params[:wood\_cabinets] %\> Wood kitchen cabinets |
| \<%=check\_box\_tag :wood\_windows\_doors, 1, params[:wood\_windows\_doors] %\> Wood windows and doors |
| \<%=check\_box\_tag :wood\_flooring, 1, params[:wood\_flooring] %\> Wood flooring |
| | \<%= submit\_tag "search", :name =\> nil %\> |

 |
