# Test returned value by sql statement

**URL:** <https://www.ruby-forum.com/t/test-returned-value-by-sql-statement/138268>\
**Category:** Rails\
**Created:** [May 21, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/test-returned-value-by-sql-statement/138268 "2008-05-21T15:41:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [May 21, 2008, 3:41pm UTC](https://www.ruby-forum.com/t/test-returned-value-by-sql-statement/138268/1 "2008-05-21T15:41:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [May 21, 2008, 3:47pm UTC](https://www.ruby-forum.com/t/test-returned-value-by-sql-statement/138268/2 "2008-05-21T15:47:17Z")

</div>

On 21 May 2008, at 14:40, joel wrote:

> 

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

---

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [May 21, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/test-returned-value-by-sql-statement/138268/3 "2008-05-21T15:50:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9bd4f/32.png) [@joel](https://www.ruby-forum.com/u/joel)\
**Post date:** [May 21, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/test-returned-value-by-sql-statement/138268/4 "2008-05-21T16:30:07Z")

</div>

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.

–  
Phlip
