Forum: Ruby on Rails wrong number of arguments (2 for 1)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
24b05e200310d4673bf93e9349067887?d=identicon&s=25 Paul Sturgess (Guest)
on 2006-04-11 14:40
(Received via mailing list)

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 = @departments.find(:all, :conditions => ["category = ?",

If anyone could shed some light on this it would be greatly appreciated.

5d15c6821f3c3054c04b85471824ba7c?d=identicon&s=25 Richard Williams (Guest)
on 2006-04-11 15:04
(Received via mailing list)
>@departments = Department.find(:all, :conditions => ["permalink = ?",
>@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])
13ea48f056b19349027d2f4a6921a46a?d=identicon&s=25 Jean-François (Guest)
on 2006-04-11 15:10
(Received via mailing list)
Hello Paul,

> @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.
This topic is locked and can not be replied to.