Forum: Ruby on Rails Retrieving text from a query fired in Rails

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
4caae45b287886770f0e87de4a8669c1?d=identicon&s=25 Aman Thind (Guest)
on 2007-05-23 07:46

I have a query that returns a number and I want to use that number after

Could you please let me know how to access the data.

I fire the query like:

numscens = Progression.find_by_sql("select numscenarios from
progressions where id = #{params[:progressionid]}")

How can I access and use the numscenarios returned by the query that is
part of my numscens variable now.

If I do a numscens.to_xml I get:

<?xml version="1.0" encoding="UTF-8"?>
    <numscenarios type="integer">2</numscenarios>

Still I cannot figure out how to access that number in numscenarios tag.

I want to do if (numscens == 1) do this else that...

Apologies for the novice question.

Many Thanks,
Df4978da52459529e539da4c995918cb?d=identicon&s=25 Zach Inglis // LT3media (Guest)
on 2007-05-23 07:59
(Received via mailing list)
Why don't you just use:

numscens = Progression.find(:first, :condition => :id => params
[:progressionid]), or even simpler:
numscens = Progression.find(params[:progressionid])

I believe, inserting the params directly like that can make you open
for SQL injection attacks.

And then, to use it from there, just use: numscens.

If you want to make it available to your view, use:
@numscens = ....
then use <%= @numscens %> anywhere in your view
This topic is locked and can not be replied to.