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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-08 16:51 UTC
Re: wrong number of arguments (0 for 1) error.... Help!!!
On 8 Dec 2008, at 16:30, Jay Mark wrote:> > 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, @authors = Author.find_all_by_name name would work just fine Fred> Please help!!!! > > Cypray > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung wrote:> On 8 Dec 2008, at 16:30, Jay Mark 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-08 20:05 UTC
Re: wrong number of arguments (0 for 1) error.... Help!!!
On Dec 8, 7:31 pm, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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> @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 Cheung wrote: > > On 8 Dec 2008, at 16:30, Jay Mark 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 > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung wrote:> On Dec 8, 7:31�pm, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-08 23:30 UTC
Re: wrong number of arguments (0 for 1) error.... Help!!!
On 8 Dec 2008, at 22:53, Jay Mark wrote:> > 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. Fred> I can''t find it in the Docs. > Please send me any pointers. > Thanks > > Cypray > > > > > > > > > > > > Frederick Cheung wrote: >> On Dec 8, 7:31�pm, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> 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 > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Bigg Freelancer http://frozenplague.net On 09/12/2008, at 9:30 AM, Frederick Cheung wrote:> > > On 8 Dec 2008, at 22:53, Jay Mark wrote: > >> >> 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. > > Fred >> I can''t find it in the Docs. >> Please send me any pointers. >> Thanks >> >> Cypray >> >> >> >> >> >> >> >> >> >> >> >> Frederick Cheung wrote: >>> On Dec 8, 7:31�pm, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >>> 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 >> >> -- >> Posted via http://www.ruby-forum.com/. >> >>> > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-09 00:29 UTC
Re: wrong number of arguments (0 for 1) error.... Help!!!
On 8 Dec 2008, at 23:36, Ryan Bigg 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 Bigg > Freelancer > http://frozenplague.net > > > > > > > > On 09/12/2008, at 9:30 AM, Frederick Cheung wrote: > >> >> >> On 8 Dec 2008, at 22:53, Jay Mark wrote: >> >>> >>> 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. >> >> Fred >>> I can''t find it in the Docs. >>> Please send me any pointers. >>> Thanks >>> >>> Cypray >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> Frederick Cheung wrote: >>>> On Dec 8, 7:31�pm, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >>>> 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 >>> >>> -- >>> Posted via http://www.ruby-forum.com/. >>> >>>> >> >> >>> > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung wrote:> On 8 Dec 2008, at 23:36, Ryan Bigg 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg (Radar)
2008-Dec-09 03:07 UTC
Re: wrong number of arguments (0 for 1) error.... Help!!!
Stop using find_by_sql! You can just do: author = Author.find_by_name(params[:author][:name]) books = author.books.find(:all, :select => "title") --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Bigg 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")-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
pastie your models please ----- Ryan Bigg Freelancer http://frozenplague.net On 09/12/2008, at 2:14 PM, Jay Mark wrote:> > 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 Bigg 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") > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Here are my models: class Author < ActiveRecord::Base belongs_to :book end class Book < ActiveRecord::Base has_many :authors end Ryan Bigg wrote:> pastie your models please > ----- > Ryan Bigg > Freelancer > http://frozenplague.net-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
And in the console do Author.new and Book.new and show me the output of each please. ----- Ryan Bigg Freelancer http://frozenplague.net On 09/12/2008, at 2:44 PM, Jay Mark wrote:> > Here are my models: > > class Author < ActiveRecord::Base > > > belongs_to :book > > end > > class Book < ActiveRecord::Base > > > > > has_many :authors > > > end > > > > > > > > Ryan Bigg wrote: >> pastie your models please >> ----- >> Ryan Bigg >> Freelancer >> http://frozenplague.net > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It says: Author.new is not recognized as an internal or external command, operable program or batch file. same for Book. Ryan Bigg wrote:> And in the console do Author.new and Book.new and show me the output > of each please. > ----- > Ryan Bigg > Freelancer > http://frozenplague.net-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I meant doing it in ruby script/console, not just in the command ----- Ryan Bigg Freelancer http://frozenplague.net On 09/12/2008, at 3:03 PM, Jay Mark wrote:> > It says: > Author.new is not recognized as an internal or external command, > operable program or batch file. > > same for Book. > > > > > Ryan Bigg wrote: >> And in the console do Author.new and Book.new and show me the output >> of each please. >> ----- >> Ryan Bigg >> Freelancer >> http://frozenplague.net > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I am using InstantRails. All I have is the command The "Open Ruby Console Window" gives me the command window Ryan Bigg wrote:> I meant doing it in ruby script/console, not just in the command > ----- > Ryan Bigg > Freelancer > http://frozenplague.net-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Bigg Freelancer http://frozenplague.net On 09/12/2008, at 3:16 PM, Jay Mark wrote:> > I am using InstantRails. All I have is the command > The "Open Ruby Console Window" gives me the command window > > > > Ryan Bigg wrote: >> I meant doing it in ruby script/console, not just in the command >> ----- >> Ryan Bigg >> Freelancer >> http://frozenplague.net > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Bigg 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 Bigg > Freelancer > http://frozenplague.net-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
And finally, what version of Rails? ----- Ryan Bigg Freelancer http://frozenplague.net On 09/12/2008, at 11:11 PM, Jay Mark wrote:> > 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 Bigg 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 Bigg >> Freelancer >> http://frozenplague.net > > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> And finally, what version of Rails? > ----- > Ryan Bigg > Freelancer > http://frozenplague.netI am using 2.0.2 with mysql database Cypray -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I have no clue. Author.find_by_name should be working when it (apparently) is not. Something is broken. ----- Ryan Bigg Freelancer http://frozenplague.net On 10/12/2008, at 8:44 AM, Jay Mark wrote:> > Ryan Bigg wrote: >> And finally, what version of Rails? >> ----- >> Ryan Bigg >> Freelancer >> http://frozenplague.net > > I am using 2.0.2 with mysql database > > Cypray > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg wrote:> I have no clue. Author.find_by_name should be working when it > (apparently) is not. Something is broken. > ----- > Ryan Bigg > Freelancer > http://frozenplague.netI 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Dec-10 08:59 UTC
Re: wrong number of arguments (0 for 1) error.... Help!!!
On Dec 10, 5:36 am, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Ryan Bigg wrote: > > I have no clue. Author.find_by_name should be working when it > > (apparently) is not. Something is broken. > > ----- > > Ryan Bigg > > 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? >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung wrote:> On Dec 10, 5:36�am, Jay Mark <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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. > > FredThanks a lot Fred. It works perfect now. Thanks for your help guys. Cypray -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---