# nOOb question: How to use find\_all with form input data

**URL:** <https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545>\
**Category:** Rails\
**Created:** [April 22, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545 "2006-04-22T23:14:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@noob](https://www.ruby-forum.com/u/noob)\
**Post date:** [April 22, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545/1 "2006-04-22T23:14:26Z")

</div>

Hello,

I’m having a problem utilizing the find\_all method with a value from a  
form. I keep getting the following error:

Mysql::Error: #42S22Unknown column ‘category\_id11’ in ‘where clause’:  
SELECT \* FROM items WHERE (category\_id11)

The controller seems to be getting the correct data, but my key and  
value seem to be mashed together(it’s mashed-hashÂ© )

I’m using a pulldown box that contains a list of the categories. I’ve  
tried some simple variations, but can’t seem to get it. It has to be  
something silly. I included the form code at the bottom.

Here’s the controller:

def listItemsInCategory  
@items = Item.find\_all(params[:item])  
end

Here’s the form:

\<%= start\_form\_tag :action =\> “listItemsInCategory”, :id =\>@category%\>

| categoryPulldown  
\<% @items.each do |category| %\> \> \<%= category.name %\> \<% end %\> | \<%= submit\_tag "find"%\> |

Thoughts???

---

<div class="post-metadata">

**Author:** ![noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@noob](https://www.ruby-forum.com/u/noob)\
**Post date:** [April 22, 2006, 11:31pm UTC](https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545/2 "2006-04-22T23:31:01Z")

</div>

nOOb,

I could be wrong here, but I don’t think that find\_all wants a hash. If  
you  
are just trying to get all of the records use Item.find\_all. If you  
need to  
use conditions you can do Item.find(:all, :conditions =\>  
“place-condition-here”) or even something like  
Item.find\_all\_by\_category\_id(  
params[:category\_id] )

Rob

---

<div class="post-metadata">

**Author:** ![noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@noob](https://www.ruby-forum.com/u/noob)\
**Post date:** [April 22, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545/3 "2006-04-22T23:34:31Z")

</div>

nOOb wrote:

> Hello,
> 
> I’m having a problem utilizing the find\_all method with a value from a  
> form. I keep getting the following error:
> 
> Mysql::Error: #42S22Unknown column ‘category\_id11’ in ‘where clause’:  
> SELECT \* FROM items WHERE (category\_id11)
> 
> The controller seems to be getting the correct data, but my key and  
> value seem to be mashed together(it’s mashed-hashÂ© )
> 
> I’m using a pulldown box that contains a list of the categories. I’ve  
> tried some simple variations, but can’t seem to get it. It has to be  
> something silly. I included the form code at the bottom.
> 
> Here’s the controller:
> 
> def listItemsInCategory  
> @items = Item.find\_all(params[:item])  
> end
> 
> Here’s the form:
> 
> \<%= start\_form\_tag :action =\> “listItemsInCategory”, :id =\>@category%\>
> 
> | categoryPulldown  
> \<% @items.each do |category| %\> \> \<%= category.name %\> \<% end %\> | \<%= submit\_tag "find"%\> |
> 
> Thoughts???

Something else must be going on. The only code in there that queries  
the database is in the controller’s find\_all. Nothing in that view  
should require a databse call, therefore you should not get a databse  
error. Double check your stack trace to find exactly which line of your  
code is making this happen.

---

<div class="post-metadata">

**Author:** ![noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@noob](https://www.ruby-forum.com/u/noob)\
**Post date:** [April 23, 2006, 1:41am UTC](https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545/4 "2006-04-23T01:41:43Z")

</div>

Rob M. wrote:

> nOOb,
> 
> I could be wrong here, but I don’t think that find\_all wants a hash. If  
> you  
> are just trying to get all of the records use Item.find\_all. If you  
> need to  
> use conditions you can do Item.find(:all, :conditions =\>  
> “place-condition-here”) or even something like  
> Item.find\_all\_by\_category\_id(  
> params[:category\_id] )
> 
> Rob

Hmmm…

The conditions are the problem. I can easily pull ALL of the records.

What I need to do is pull the records based on the selected field of the  
dropdown menu in my form.

To put it in context, your “place-conditions-here” is the part I’m  
trying to figre out. How do I pull those conditions out of my passed  
form data?

I think that “Item.find\_all\_by\_category\_id(params[:category\_id])”  
translates to:  
SELECT \* FROM items WHERE category\_id LIKE ‘category\_id11’  
It doesn’t generate an error, but if pulls no rows either.

Thanks though…

---

<div class="post-metadata">

**Author:** ![noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@noob](https://www.ruby-forum.com/u/noob)\
**Post date:** [April 23, 2006, 3:01am UTC](https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545/5 "2006-04-23T03:01:47Z")

</div>

Rob M. wrote:

> Can you post your stack trace?

Thanks for your interest guys!

The database call is in the controller definition:

> def listItemsInCategory  
> @items = Item.find\_all(params[:item])  
> end

Here’s the full message:

c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/connection\_adapters/abstract\_adapter.rb:88:in  
`log' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/mysql_adapter.rb:180:in`execute’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/connection\_adapters/mysql\_adapter.rb:322:in  
`select' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/connection_adapters/mysql_adapter.rb:171:in`select\_all’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:431:in  
`find_by_sql' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:395:in`find’  
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/deprecated\_finders.rb:37:in  
`find_all' #{RAILS_ROOT}/app/controllers/categories_controller.rb:132:in`listItemsInCategory’

Here’s two examples of the request that is generated by selecting  
diffent items in the pulldown menu:

Request  
Parameters: {“commit”=\>“find”, “item”=\>{“category\_id”=\>“7”}, “id”=\>“11”}

Request  
Parameters: {“commit”=\>“find”, “item”=\>{“category\_id”=\>“9”}, “id”=\>“11”}

This seems like it should be simple…I’m soooo frustrated.

Thanks again…

---

<div class="post-metadata">

**Author:** ![noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@noob](https://www.ruby-forum.com/u/noob)\
**Post date:** [April 23, 2006, 1:46am UTC](https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545/6 "2006-04-23T01:46:04Z")

</div>

Can you post your stack trace?

---

<div class="post-metadata">

**Author:** ![noob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/5e9695/32.png) [@noob](https://www.ruby-forum.com/u/noob)\
**Post date:** [April 23, 2006, 3:26am UTC](https://www.ruby-forum.com/t/noob-question-how-to-use-find-all-with-form-input-data/58545/7 "2006-04-23T03:26:59Z")

</div>

I was able to replicate your error and the way I got around it was doing  
this:

id = params[:item][:category\_id]  
@items = Item.find\_all(“category\_id = ‘#{id}’”)

You would image that just doing Item.find\_all(params[:item]) would work,  
but  
I guess not…

Rob
