Wrong number of arguments (0 for 1) error.... Help!

I am using Rails 2.0.2 with mysql database
I am getting this error when running a query with Find_by_sql:

“wrong number of arguments (0 for 1)”

Here are my queries:

@authors = Author.find_by_sql[“SELECT * FROM authors WHERE name = ?’,
name”]
Also
@books = Books.find_by_sql[“SELECT * FROM books where title IN( SELECT
title FROM authors where name = ?’, name)”]

I am passing in ‘name’ as a parameter and the URL shows the right
parameter.

Is the syntax of my queries correct? What can cause this error?
I use partial form and I still get the error.

Please help!!!

Cypray

On 8 Dec 2008, at 16:30, Jay M. wrote:

Also
@books = Books.find_by_sql[“SELECT * FROM books where title IN( SELECT
title FROM authors where name = ?’, name)”]

I am passing in ‘name’ as a parameter and the URL shows the right
parameter.

Is the syntax of my queries correct? What can cause this error?
I use partial form and I still get the error.

Your syntax isn’t right in two ways. You need a space before the [ or
ruby thinks that you want to call the find_by_sql methods with no
arguments and then call the [] method on the result, and seconly
what’s in that array isn’t right,

@authors = Author.find_by_sql[“SELECT * FROM authors WHERE name = ?’,
name”]

should be

@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
name]

and arguably you shouldn’t be using find_by_sql at all here,

@authors = Author.find_all_by_name name

would work just fine

Fred

Thanks for your help Fred. The wrong number of argument problem is gone.

The queries are still not working.

Using find_all_by_name give me “method not found error”

This one below returns no data but gives no error. But it works fine
when I put it in mysql directly.

@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
‘name’]

Also passing it in as author[:name] returns no data but gives no error,
just empty rows

@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
‘author[:name]’]

Without the single quote (’’) around the ‘name’, I get “undefined local
variable” error.

Does any one know what is wrong with my query?

Also Rails is rejecting the syntax of the subselect below:

@books = Books.find_by_sql [“SELECT * FROM books where title IN( SELECT
title FROM authors where name = ?”, ‘name’)]

It is not accepting ‘(’ or ‘[’ to enclose the inner SELECT

I have tested this also in mysql and it works fine.

Does any one know the correct syntax for a subselect in Rails?

Thanks
Cypray.

Frederick C. wrote:

On 8 Dec 2008, at 16:30, Jay M. wrote:

Also
@books = Books.find_by_sql[“SELECT * FROM books where title IN( SELECT
title FROM authors where name = ?’, name)”]

I am passing in ‘name’ as a parameter and the URL shows the right
parameter.

Is the syntax of my queries correct? What can cause this error?
I use partial form and I still get the error.

Your syntax isn’t right in two ways. You need a space before the [ or
ruby thinks that you want to call the find_by_sql methods with no
arguments and then call the [] method on the result, and seconly
what’s in that array isn’t right,

@authors = Author.find_by_sql[“SELECT * FROM authors WHERE name = ?’,
name”]

should be

@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
name]

and arguably you shouldn’t be using find_by_sql at all here,

@authors = Author.find_all_by_name name

would work just fine

Fred

On Dec 8, 7:31 pm, Jay M. [email protected] wrote:

Thanks for your help Fred. The wrong number of argument problem is gone.

The queries are still not working.

Using find_all_by_name give me “method not found error”

That should work as long as the argument you pass it exists (and as
long as Author has a column called name) , but from what you say below
you don’t have a local variable called name.

This one below returns no data but gives no error. But it works fine
when I put it in mysql directly.

@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
‘name’]

Also passing it in as author[:name] returns no data but gives no error,
just empty rows

Well you’ve asked for authors whose name is the exact string ‘name’.
Presumably you have a variable of some sort with the name you are
searching for ? You should stick that in

@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
‘author[:name]’]

Without the single quote (‘’) around the ‘name’, I get “undefined local
variable” error.

Does any one know what is wrong with my query?

Also Rails is rejecting the syntax of the subselect below:

You’re closing the parens in the wrong place. what you pass to
find_by_sql must be
[ “A string with all the sql”, all, the, substitutions, here]

take a look at the examples in the docs.

Fred

Thanks again Fred.
NO, I am not asking for the author whose name is the exact string
‘name’.
“Name” is the attribute or column in the authors table which has name
values like James, John, etc. The user will select a name, say John,
then I am passing that name as a condition to the query.
After a submit button is clicked, the URL shows this:
http://localhost:3000/authors/show?author[name]=John&commit=Submit

I can’t stick the name value in because it has to come dynamically to
the controller. Name is not unique in the table

I am still looking for the correct syntax for this:
@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
‘author[:name]’]

and this subselect:

@books = Books.find_by_sql [“SELECT * FROM books where title IN( SELECT
title FROM authors where name = ?”, ‘name’)]

I can’t find it in the Docs.
Please send me any pointers.
Thanks

Cypray

Frederick C. wrote:

On Dec 8, 7:31�pm, Jay M. [email protected] wrote:

Thanks for your help Fred. The wrong number of argument problem is gone.

The queries are still not working.

Using find_all_by_name give me “method not found error”

That should work as long as the argument you pass it exists (and as
long as Author has a column called name) , but from what you say below
you don’t have a local variable called name.

This one below returns no data but gives no error. But it works fine
when I put it in mysql directly.

@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
�’name’]

Also passing it in as author[:name] returns no data but gives no error,
just empty rows

Well you’ve asked for authors whose name is the exact string ‘name’.
Presumably you have a variable of some sort with the name you are
searching for ? You should stick that in

@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
‘author[:name]’]

Without the single quote (‘’) around the ‘name’, I get “undefined local
variable” error.

Does any one know what is wrong with my query?

Also �Rails is rejecting the syntax of the subselect below:

You’re closing the parens in the wrong place. what you pass to
find_by_sql must be
[ “A string with all the sql”, all, the, substitutions, here]

take a look at the examples in the docs.

Fred

If it’s coming in dynamically, wouldn’t it be params[:author][:name]
or something similar? A bit hard to guess without seeing more code.

Ryan B.
Freelancer

On 8 Dec 2008, at 23:36, Ryan B. wrote:

If it’s coming in dynamically, wouldn’t it be params[:author][:name]
or something similar? A bit hard to guess without seeing more code.

D’oh of course, not sure why I wrote author[:name]

Fred

Thanks guys, the first query is working fine now.

Rails is still rejecting the syntax of this subselect:

@books = Books.find_by_sql [“SELECT * FROM books WHERE title IN SELECT
title FROM authors WHERE name = ?”, params[:author][:name]]

I know the inner select should be in (), but its not taking it where I
put it.
Can any one see what the problem is?
Thanks
Cypray

Frederick C. wrote:

On 8 Dec 2008, at 23:36, Ryan B. wrote:

If it’s coming in dynamically, wouldn’t it be params[:author][:name]
or something similar? A bit hard to guess without seeing more code.

D’oh of course, not sure why I wrote author[:name]

Fred

Stop using find_by_sql!

You can just do:

author = Author.find_by_name(params[:author][:name])
books = author.books.find(:all, :select => “title”)

On 8 Dec 2008, at 22:53, Jay M. wrote:

I can’t stick the name value in because it has to come dynamically to
the controller. Name is not unique in the table

I am still looking for the correct syntax for this:
@authors = Author.find_by_sql [“SELECT * FROM authors WHERE name = ?”,
‘author[:name]’]

Author.find_all_by_name author[:name]
In general, whereever I wrote name in the previous suggestions you can
just write author[:name]

and this subselect:

@books = Books.find_by_sql [“SELECT * FROM books where title
IN( SELECT
title FROM authors where name = ?”, ‘name’)]

Like I said, you’ve not to write it in the form
Books.find_by_sql [some_sql_statement, author[:name]]

Seems to me like you would benefit from going back to some basic ruby
stuff.

Fred

I am using find_by_sql because I get this error when I use find_by_name
or
find_all_by_name

“undefined method `find_by_name’ for #<Class:”

I get it again when I put in the code in the last post.
Is there a way to solve the undefined method problem?

Cypray

Ryan B. wrote:

Stop using find_by_sql!

You can just do:

author = Author.find_by_name(params[:author][:name])
books = author.books.find(:all, :select => “title”)

pastie your models please

Ryan B.
Freelancer

And in the console do Author.new and Book.new and show me the output
of each please.

Ryan B.
Freelancer

It says:
Author.new is not recognized as an internal or external command,
operable program or batch file.

same for Book.

Ryan B. wrote:

And in the console do Author.new and Book.new and show me the output
of each please.

Ryan B.
Freelancer
http://frozenplague.net

Here are my models:

class Author < ActiveRecord::Base

belongs_to :book

end

class Book < ActiveRecord::Base

has_many :authors

end

Ryan B. wrote:

pastie your models please

Ryan B.
Freelancer
http://frozenplague.net

I am using InstantRails. All I have is the command
The “Open Ruby Console Window” gives me the command window

Ryan B. wrote:

I meant doing it in ruby script/console, not just in the command

Ryan B.
Freelancer
http://frozenplague.net

Ok… then you’re on your own if you don’t know how to type “ruby
script/console” into a command prompt window and then type some more
stuff

Ryan B.
Freelancer

Here is the result from ruby script/console

Author.new
=> #<Author id: nil, name: “”, title: “”, book_id: nil>

Book.new
=> #<Book id: nil, title: “”, isbn: “”>

Ryan B. wrote:

Ok… then you’re on your own if you don’t know how to type “ruby
script/console” into a command prompt window and then type some more
stuff

Ryan B.
Freelancer
http://frozenplague.net

I meant doing it in ruby script/console, not just in the command

Ryan B.
Freelancer

Ryan B. wrote:

And finally, what version of Rails?

Ryan B.
Freelancer
http://frozenplague.net

I am using 2.0.2 with mysql database

Cypray