Hey- I''ve hacked together a search form with a sql query that returns some results. In the search form a user can select which genre of music they would like to search in or they can choose to search all genres. I think I''ve got the SQL right ... it returns the expected results when query the db, but when I try to recreate this same query using rails I get the following error. I''m new to all of this, but do you have any idea what I might be doing wrong? ArgumentError in Dj#search_recording malformed format string - %'' /app/controllers/dj_controller.rb:11:in `search_recording'' script/server:49 Show framework trace C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.3.0/lib/active_record/base.rb:671:in `%'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.3.0/lib/active_record/base.rb:671:in `sanitize_conditions'' C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.3.0/lib/active_record/base.rb:300:in `find_by_sql'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:585:in `send'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:585:in `perform_action_without_filters'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/filters.rb:236:in `perform_action_without_benchmark'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/benchmarking.rb:30:in `perform_action_without_rescue'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/benchmarking.rb:30:in `measure'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/benchmarking.rb:30:in `perform_action_without_rescue'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/rescue.rb:75:in `perform_action'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:261:in `send'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:261:in `process'' C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:239:in `process'' C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/dispatcher.rb:39:in `dispatch'' C:/Inetpub/wwwroot/Apl/public/dispatch.rb:10 C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:101:in `load'' C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:101:in `handle_dispatch'' C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:80:in `handle_mapped'' C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:34:in `do_POST'' C:/ruby/lib/ruby/1.8/webrick/httpservlet/abstract.rb:35:in `__send__'' C:/ruby/lib/ruby/1.8/webrick/httpservlet/abstract.rb:35:in `service'' C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:97:in `service'' C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:58:in `run'' C:/ruby/lib/ruby/1.8/webrick/server.rb:153:in `start_thread'' C:/ruby/lib/ruby/1.8/webrick/server.rb:147:in `start'' C:/ruby/lib/ruby/1.8/webrick/server.rb:147:in `start_thread'' C:/ruby/lib/ruby/1.8/webrick/server.rb:97:in `start'' C:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'' C:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'' C:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' C:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:19:in `dispatch'' Request Parameters: {"search_genre"=>"%", "search_term"=>"cameo", "search_acquired"=>"631138519", "search_in"=>"recording_artist"} Show session dump --- {} Response Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"} Show template parameters HERE IS THE SQL THAT SEEMS TO WORK: SELECT * FROM recordings WHERE MATCH (recording_artist,recording_title,recording_label,recording_release_date) AGAINST (''cameo'') AND recording_genre LIKE ''%'' AND created_at<=''Sun Dec 29 21:32:20 Eastern Standard Time 2024'' HERE IS THE RAILS CODE: @results = Recording.find_by_sql ["SELECT * FROM recordings WHERE MATCH (" + @params[''search_in''] + ") AGAINST (''" + @params[''search_term''] + "'') AND recording_genre LIKE ''" + @params[''search_genre''] + "'' AND created_at<=''" + time_span.to_s + "''" ]
On 29 Dec 2004, at 23:25, Keith Donaldson wrote:> I think I''ve got the SQL right ... it returns the expected results > when query the db, but when I try to recreate this same query using > rails I get the following error. I''m new to all of this, but do you > have any idea what I might be doing wrong? > > ArgumentError in Dj#search_recording > > malformed format string - %''Escape your % with %%, Rails uses sprintf (%) on the inside.> /app/controllers/dj_controller.rb:11:in `search_recording'' > script/server:49 > > Show framework trace > > lib/active_record/base.rb:671:in `%''^^^ Your clue -- Eric Hodel - drbrain-48TerJ1FxhPk1uMJSBkQmQ@public.gmane.org - http://segment7.net FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Keith Donaldson wrote:> @results = Recording.find_by_sql ["SELECT * FROM recordings WHERE > MATCH (" + @params[''search_in''] + ") AGAINST (''" + > @params[''search_term''] + "'') AND recording_genre LIKE ''" + > @params[''search_genre''] + "'' AND created_at<=''" + time_span.to_s + "''" > ]This is dangerous, please read http://manuals.rubyonrails.com/read/chapter/43.
>From one of my projects:def self.search(query, limit = 20, offset = 0) find_by_sql(["SELECT posts.*, feeds.name as feedname, feeds.author as authorname FROM posts, feeds WHERE feeds.id=posts.feed_id AND MATCH (posts.title,posts.body) AGAINST (? IN BOOLEAN MODE) ORDER BY posts.created_at DESC LIMIT ? OFFSET ?", query, limit, offset]) end On Thu, 30 Dec 2004 02:25:23 -0500, Keith Donaldson <keith.donaldson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey- > > I''ve hacked together a search form with a sql query that returns some > results. In the search form a user can select which genre of music > they would like to search in or they can choose to search all genres. > > I think I''ve got the SQL right ... it returns the expected results > when query the db, but when I try to recreate this same query using > rails I get the following error. I''m new to all of this, but do you > have any idea what I might be doing wrong? > > ArgumentError in Dj#search_recording > > malformed format string - %'' > > /app/controllers/dj_controller.rb:11:in `search_recording'' > script/server:49 > > Show framework trace > > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.3.0/lib/active_record/base.rb:671:in > `%'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.3.0/lib/active_record/base.rb:671:in > `sanitize_conditions'' > C:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.3.0/lib/active_record/base.rb:300:in > `find_by_sql'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:585:in > `send'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:585:in > `perform_action_without_filters'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/filters.rb:236:in > `perform_action_without_benchmark'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/benchmarking.rb:30:in > `perform_action_without_rescue'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/benchmarking.rb:30:in > `measure'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/benchmarking.rb:30:in > `perform_action_without_rescue'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/rescue.rb:75:in > `perform_action'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:261:in > `send'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:261:in > `process'' > C:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.1.0/lib/action_controller/base.rb:239:in > `process'' > C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/dispatcher.rb:39:in `dispatch'' > C:/Inetpub/wwwroot/Apl/public/dispatch.rb:10 > C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:101:in `load'' > C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:101:in > `handle_dispatch'' > C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:80:in > `handle_mapped'' > C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:34:in `do_POST'' > C:/ruby/lib/ruby/1.8/webrick/httpservlet/abstract.rb:35:in `__send__'' > C:/ruby/lib/ruby/1.8/webrick/httpservlet/abstract.rb:35:in `service'' > C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:97:in `service'' > C:/ruby/lib/ruby/1.8/webrick/httpserver.rb:58:in `run'' > C:/ruby/lib/ruby/1.8/webrick/server.rb:153:in `start_thread'' > C:/ruby/lib/ruby/1.8/webrick/server.rb:147:in `start'' > C:/ruby/lib/ruby/1.8/webrick/server.rb:147:in `start_thread'' > C:/ruby/lib/ruby/1.8/webrick/server.rb:97:in `start'' > C:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `each'' > C:/ruby/lib/ruby/1.8/webrick/server.rb:89:in `start'' > C:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' > C:/ruby/lib/ruby/1.8/webrick/server.rb:79:in `start'' > C:/ruby/lib/ruby/gems/1.8/gems/rails-0.9.2/lib/webrick_server.rb:19:in > `dispatch'' > > Request > > Parameters: {"search_genre"=>"%", "search_term"=>"cameo", > "search_acquired"=>"631138519", "search_in"=>"recording_artist"} > > Show session dump > > --- {} > > Response > Headers: {"cookie"=>[], "Cache-Control"=>"no-cache"} > > Show template parameters > > HERE IS THE SQL THAT SEEMS TO WORK: > > SELECT * FROM recordings WHERE MATCH > (recording_artist,recording_title,recording_label,recording_release_date) > AGAINST (''cameo'') AND recording_genre LIKE ''%'' AND created_at<=''Sun > Dec 29 21:32:20 Eastern Standard Time 2024'' > > HERE IS THE RAILS CODE: > > @results = Recording.find_by_sql ["SELECT * FROM recordings WHERE > MATCH (" + @params[''search_in''] + ") AGAINST (''" + > @params[''search_term''] + "'') AND recording_genre LIKE ''" + > @params[''search_genre''] + "'' AND created_at<=''" + time_span.to_s + "''" > ] > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://blog.leetsoft.com