Test returned value by sql statement

Hello people,

just wanted to know how to actually test returned value from sql statement like :

@alreadyused = Promotioncodes.find_by_sql [ "select alreadyused from promotioncodes where id = ?", @codeid ]

I would like to check whether @alreadyused has value 1 or 0

It doesn't seem a simple if @alreadyused would word ...

Regards

Joel

Hello people,

just wanted to know how to actually test returned value from sql statement like :

@alreadyused = Promotioncodes.find_by_sql [ "select alreadyused from promotioncodes where id = ?", @codeid ]

I would like to check whether @alreadyused has value 1 or 0

find_by_sql returns an array of model objects: so

foo = Promotioncodes.find_by_sql [ "select alreadyused from
promotioncodes where id = ?", @codeid ]

if foo.first.alreadyused? ... end

Also there's no need to break out find_by_sql for that foo = Promotioncodes.find @codeid if foo.alreadyused? ... end

is more than enough (and in keeping with rails conversion that model
would be Promotioncode (or even PromotionCode))

Fred

Ok thank you very much,

Also wanted to know while using update_attribute method, how to actually test that the attribute has effectively been updated ? ( need to test to trigger appropriate popup message )

Thanks again,

Joel

joel wrote:

just wanted to know how to actually test returned value from sql statement like :

@alreadyused = Promotioncodes.find_by_sql [ "select alreadyused from promotioncodes where id = ?", @codeid ]

I would like to check whether @alreadyused has value 1 or 0

   count_by_sql

next, what's wrong with

   PromotionCode.find_by_id(@codeid).alreadyused

?

I would guess what's wrong with it is your model names don't follow Rails conventions, so you have not yet tuned your ActiveRecord calls to match them.