Hi, Im having trouble figuring out how to query my database in ruby and i''m hoping someone can help me out. I''d like to find all my departments that match a permalink passed from the url. The department must also match a particular category, also passed through the url. I have written the following code. But unfortunatley I get an error "wrong number of arguments (2 for 1)" category = params[:category] department = params[:department] @departments = Department.find(:all, :conditions => ["permalink = ?", department]) @department = @departments.find(:all, :conditions => ["category = ?", category]) If anyone could shed some light on this it would be greatly appreciated. Thanks, Paul. -- _________________________ http://www.paulsturgess.co.uk
>I''d like to find all my departments that match a permalink passed from >the url. The department must also match a particular category, also >passed through the url. > >I have written the following code. But unfortunatley I get an error >"wrong number of arguments (2 for 1)" > >category = params[:category] >department = params[:department] > >@departments = Department.find(:all, :conditions => ["permalink = ?", >department]) >@department = @departments.find(:all, :conditions => ["category >?", category])Is this an exact copy of the code? The find :all will return a hash even if there is only one result row. So in the above @department will always be a hash. If you want only one department then use find :first which returns a scaler of the row. As an aside you can also combine the two statements: @department = Department.find(:first, :conditions => ["permalink = ? and category = ?", department, category]) -- Posted with http://DevLists.com. Sign up and save your mailbox.
Hello Paul,> I''d like to find all my departments that match a permalink passed > from the url. The department must also match a particular category, > also passed through the url. > > I have written the following code. But unfortunatley I get an error > "wrong number of arguments (2 for 1)" > > category = params[:category] > department = params[:department] > > @departments = Department.find(:all, :conditions => ["permalink = ?", > department]) > @department = @departments.find(:all, :conditions => ["category = ?", category])@departments is an array of Department objects created by the class method Department#find. In the second line, since @departments is an array, you are using Enumerable#find which require 0 or 1 argument and 1 block. So that''s not the find method you want to use, and that''s not the right way to use Enumerable#find as well :) so you''d better do : @departments = Department.find(:all, :conditions => ["permalink = :department AND category = :category", params ]) -- Jean-Fran?ois. -- ? la renverse.
The code below gives me the following error: wrong number of arguments (2 for 1) It goes though this code twice. The first everything works and it prints: Q*** Action name is index Q*** Controller name is welcome protect? welcome / index The second time, I get this error and it prints: Q*** Action name is signup Q*** Controller name is user It seems like it should work. Any ideas? module UserSystem ... def protect?(controller, action) puts "protect? #{controller} / #{action}" if [''welcome'', ''user''].include?(controller) return false else return true end end def login_required puts "Q*** Action name is #{action_name}" puts "Q*** Controller name is #{controller_name}" if not protect?(controller_name, action_name) return true end if user? and authorize?(@session[''user'']) return true end -- Posted via http://www.ruby-forum.com/.
The code below gives me the following error: wrong number of arguments (2 for 1) It goes though this code twice. The first everything works and it prints: Q*** Action name is index Q*** Controller name is welcome protect? welcome / index The second time, I get this error and it prints: Q*** Action name is signup Q*** Controller name is user It seems like it should work. Any ideas? module UserSystem ... def protect?(controller, action) puts "protect? #{controller} / #{action}" if [''welcome'', ''user''].include?(controller) return false else return true end end def login_required puts "Q*** Action name is #{action_name}" puts "Q*** Controller name is #{controller_name}" if not protect?(controller_name, action_name) return true end if user? and authorize?(@session[''user'']) return true end -- Posted via http://www.ruby-forum.com/.