Forum: Ruby on Rails wrong number of arguments (0 for 1) error.... Help!!!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jay M. (Guest)
on 2008-12-08 18:30
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
Frederick C. (Guest)
on 2008-12-08 18:52
(Received via mailing list)
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
Jay M. (Guest)
on 2008-12-08 21:31
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
Frederick C. (Guest)
on 2008-12-08 22:05
(Received via mailing list)
On Dec 8, 7:31 pm, Jay M. <removed_email_address@domain.invalid> 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
Jay M. (Guest)
on 2008-12-09 00:53
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]=Jo...

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. <removed_email_address@domain.invalid> 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
Frederick C. (Guest)
on 2008-12-09 01:31
(Received via mailing list)
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
Ryan B. (Guest)
on 2008-12-09 01:36
(Received via mailing list)
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
http://frozenplague.net
Frederick C. (Guest)
on 2008-12-09 02:29
(Received via mailing list)
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
Jay M. (Guest)
on 2008-12-09 05:03
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
Ryan B. (Guest)
on 2008-12-09 05:07
(Received via mailing list)
Stop using find_by_sql!

You can just do:

author = Author.find_by_name(params[:author][:name])
books = author.books.find(:all, :select => "title")
Jay M. (Guest)
on 2008-12-09 06:14
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")
Ryan B. (Guest)
on 2008-12-09 06:24
(Received via mailing list)
pastie your models please
-----
Ryan B.
Freelancer
http://frozenplague.net
Jay M. (Guest)
on 2008-12-09 06:44
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
Ryan B. (Guest)
on 2008-12-09 06:46
(Received via mailing list)
And in the console do Author.new and Book.new and show me the output
of each please.
-----
Ryan B.
Freelancer
http://frozenplague.net
Jay M. (Guest)
on 2008-12-09 07:03
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
Ryan B. (Guest)
on 2008-12-09 07:10
(Received via mailing list)
I meant doing it in ruby script/console, not just in the command
-----
Ryan B.
Freelancer
http://frozenplague.net
Jay M. (Guest)
on 2008-12-09 07:16
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
Ryan B. (Guest)
on 2008-12-09 08:32
(Received via mailing list)
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
Jay M. (Guest)
on 2008-12-09 15:11
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
Ryan B. (Guest)
on 2008-12-10 00:05
(Received via mailing list)
And finally, what version of Rails?
-----
Ryan B.
Freelancer
http://frozenplague.net
Jay M. (Guest)
on 2008-12-10 00:44
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
Ryan B. (Guest)
on 2008-12-10 05:52
(Received via mailing list)
I have no clue. Author.find_by_name should be working when it
(apparently) is not. Something is broken.
-----
Ryan B.
Freelancer
http://frozenplague.net
Jay M. (Guest)
on 2008-12-10 07:36
Ryan B. wrote:
> I have no clue. Author.find_by_name should be working when it
> (apparently) is not. Something is broken.
> -----
> Ryan B.
> Freelancer
> http://frozenplague.net

I think so too. Thanks for your help.
Why is it so hard to find the correct syntax for doing a subselect in
Rails?
Is subselect not supported in Rails?

Cypray
Ryan B. (Guest)
on 2008-12-10 07:40
(Received via mailing list)
It's supported. It's the syntax I showed you before:

Author.find_by_name("Blah").books, will get you all the books by that
author.
-----
Ryan B.
Freelancer
http://frozenplague.net
Frederick C. (Guest)
on 2008-12-10 10:59
(Received via mailing list)
On Dec 10, 5:36 am, Jay M. <removed_email_address@domain.invalid> wrote:
> Rails?
> Is subselect not supported in Rails?
>

For the sake of argument you can do

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

But really whenever you use find_by_sql you should give yourself  a
long hard look in the mirror. There's almost always a better way.

Fred
Jay M. (Guest)
on 2008-12-10 16:05
Frederick C. wrote:
> On Dec 10, 5:36�am, Jay M. <removed_email_address@domain.invalid> wrote:
>> Rails?
>> Is subselect not supported in Rails?
>>
>
> For the sake of argument you can do
>
> @books = Books.find_by_sql ["SELECT * FROM books WHERE title IN
> (SELECT
> title FROM authors WHERE name = ?)", params[:author][:name]]
>
> But really whenever you use find_by_sql you should give yourself  a
> long hard look in the mirror. There's almost always a better way.
>
> Fred


Thanks a lot Fred. It works perfect now. Thanks for your help guys.

Cypray
This topic is locked and can not be replied to.