# Custom SQL queries

**URL:** <https://www.ruby-forum.com/t/custom-sql-queries/152681>\
**Category:** Rails\
**Created:** [November 6, 2008, 11:55am UTC](https://www.ruby-forum.com/t/custom-sql-queries/152681 "2008-11-06T11:55:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 6, 2008, 11:55am UTC](https://www.ruby-forum.com/t/custom-sql-queries/152681/1 "2008-11-06T11:55:32Z")

</div>

Hi!

I need the results of a custom SQL query to create a list in a view.

Thats the query:

@classes = ActiveRecord::Base.connection.execute “SELECT DISTINCT CLASS  
FROM SymbolsMatch\_MASTER”

And thats the part of the view:

 \<% for class\_name in @classes do %\> - \<%= class\_name %\>
\<% end %\>

but @classes is always NULL here…

What am I doing wrong?

Thx  
lack

---

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 6, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/custom-sql-queries/152681/2 "2008-11-06T12:11:03Z")

</div>

On 6 Nov 2008, at 10:55, Günther Lackner wrote:

> 

execute just executes without returning anything. If you want the  
result then you want one of select\_all/select\_values/select\_value  
etc… (in this particular case select\_values would do the trick)

Fred

---

<div class="post-metadata">

**Author:** ![lackyg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d07c76/32.png) [@lackyg](https://www.ruby-forum.com/u/lackyg)\
**Post date:** [November 6, 2008, 12:15pm UTC](https://www.ruby-forum.com/t/custom-sql-queries/152681/3 "2008-11-06T12:15:39Z")

</div>

> @classes = ActiveRecord::Base.connection.execute “SELECT DISTINCT CLASS  
> FROM SymbolsMatch\_MASTER”

This is the trick:  
ActiveRecord::Base.connection.execute(your\_sql).extend(Enumerable).to\_a
