Forum: Ruby on Rails nOOb question: How to use find_all with form input data

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
59640faebd2831f81ed714cc95bebdbd?d=identicon&s=25 nOOb (Guest)
on 2006-04-22 23:14
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%>
<table>
<td>
<label for="item_category_id">categoryPulldown</label><br>
<select name="item[category_id]">
<% @items.each do |category| %>
<option value="<%= category.id %>"
<%= ' selected'%>>
<%= category.name %>
</option>   <% end %></select>
</td>
<td>
<%= submit_tag "find"%>
</td>
</table>


Thoughts???
A4a8fefcbd2cd6d2370609a5e2a3a389?d=identicon&s=25 Rob Merrell (Guest)
on 2006-04-22 23:31
(Received via mailing list)
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
D0cd6b10e01bacb976b3b815a9c660bc?d=identicon&s=25 Alex Wayne (Guest)
on 2006-04-22 23:34
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%>
> <table>
> <td>
> <label for="item_category_id">categoryPulldown</label><br>
> <select name="item[category_id]">
> <% @items.each do |category| %>
> <option value="<%= category.id %>"
> <%= ' selected'%>>
> <%= category.name %>
> </option>   <% end %></select>
> </td>
> <td>
> <%= submit_tag "find"%>
> </td>
> </table>
>
>
> 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.
59640faebd2831f81ed714cc95bebdbd?d=identicon&s=25 nOOb (Guest)
on 2006-04-23 01:41
Rob Merrell 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...
A4a8fefcbd2cd6d2370609a5e2a3a389?d=identicon&s=25 Rob Merrell (Guest)
on 2006-04-23 01:46
(Received via mailing list)
Can you post your stack trace?
59640faebd2831f81ed714cc95bebdbd?d=identicon&s=25 nOOb (Guest)
on 2006-04-23 03:01
Rob Merrell 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...
A4a8fefcbd2cd6d2370609a5e2a3a389?d=identicon&s=25 Rob Merrell (Guest)
on 2006-04-23 03:26
(Received via mailing list)
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
This topic is locked and can not be replied to.