On Dec 8, 7:31 pm, Jay Mark <[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

> @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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to