nOOb
2006-Apr-22 21:15 UTC
[Rails] nOOb question: How to use find_all with form input data
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??? -- Posted via http://www.ruby-forum.com/.
Rob Merrell
2006-Apr-22 21:29 UTC
[Rails] nOOb question: How to use find_all with form input data
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 On 4/22/06, nOOb <nOOb@yahoo.com> 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(c) ) > > 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??? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- c++: the power, elegance and simplicity of a hand grenade http://www.migrob.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060422/31377299/attachment.html
Alex Wayne
2006-Apr-22 21:34 UTC
[Rails] Re: nOOb question: How to use find_all with form input data
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. -- Posted via http://www.ruby-forum.com/.
nOOb
2006-Apr-22 23:41 UTC
[Rails] Re: nOOb question: How to use find_all with form input data
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] ) > > RobHmmm... 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... -- Posted via http://www.ruby-forum.com/.
Rob Merrell
2006-Apr-22 23:45 UTC
[Rails] Re: nOOb question: How to use find_all with form input data
Can you post your stack trace? On 4/22/06, nOOb <nOOb@yahoo.com> wrote:> > 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... > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- c++: the power, elegance and simplicity of a hand grenade http://www.migrob.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060422/787a187a/attachment-0001.html
nOOb
2006-Apr-23 01:01 UTC
[Rails] Re: Re: nOOb question: How to use find_all with form input d
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]) > endHere''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... -- Posted via http://www.ruby-forum.com/.
Rob Merrell
2006-Apr-23 01:26 UTC
[Rails] Re: nOOb question: How to use find_all with form input data
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 On 4/22/06, Rob Merrell <robholio@gmail.com> wrote:> > Can you post your stack trace? > > On 4/22/06, nOOb <nOOb@yahoo.com> wrote: > > > 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... > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > -- > c++: the power, elegance and simplicity of a hand grenade > http://www.migrob.com >-- c++: the power, elegance and simplicity of a hand grenade http://www.migrob.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060423/4555d543/attachment.html