nOOb question: How to use find_all with form input data


#1

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


#2

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


#3

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.


#4

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…


#5

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:inexecute’
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:inselect_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:infind’
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:inlistItemsInCategory’

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…


#6

Can you post your stack trace?


#7

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