Gee-off wrote:
> I'm trying to get the following statement to work using variable
> substitution (it works if hard-code the where (med_type) param in the
> inner SQL statement):
> 
>  med_type = 'game'
>  @mediaformats = MediaFormat.find_by_sql("select * from media_formats
> where media_formats.media_type_id = (select media_type_id from
> media_types where media_type = ?, :med_type) order by description")
> 
> The error is generated by MySQL because the query it receives is:
> 
> select * from media_formats where media_formats.media_type_id =
> (select media_type_id from media_types where media_type
> = ?, :med_type)

One other thing.  Even if you are using raw SQL, this is not a 
well-designed query.  It could be done more readably with a join. 
Subqueries are great when they're necessary, but they're necessary far 
less often than you might expect.

> 
> which indicates I have a Rails syntax error in my code.

No, it indicates that you don't quite understand the difference between 
Ruby string interpolation and SQL query parameters.

> 
> Any help appreciated - I'm using find_by_sql only because I'm more
> comfortable with SQL than Rails queries.

Unfortunately, it looks like you're comfortable with neither.  You'll 
really need to know both to use Rails profitably.
> 
> geoff

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
[email protected]
-- 
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 [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