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.
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.
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,
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]
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
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]']
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.