# SQL in rails

**URL:** <https://www.ruby-forum.com/t/sql-in-rails/152051>\
**Category:** Rails\
**Created:** [October 29, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/sql-in-rails/152051 "2008-10-29T14:29:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xxmithila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ebca7d/32.png) [@xxmithila](https://www.ruby-forum.com/u/xxmithila)\
**Post date:** [October 29, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/sql-in-rails/152051/1 "2008-10-29T14:29:23Z")

</div>

Hi all…

please give me an example that use SQL in models to handel data in  
the data base. with “sql.execute” or any other possible way…

thankx

---

<div class="post-metadata">

**Author:** ![xxmithila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ebca7d/32.png) [@xxmithila](https://www.ruby-forum.com/u/xxmithila)\
**Post date:** [October 29, 2008, 3:07pm UTC](https://www.ruby-forum.com/t/sql-in-rails/152051/2 "2008-10-29T15:07:04Z")

</div>

class Model \< ActiveRecord::Base  
def raw\_sql\_query  
connection.execute(“select name from #{table\_name}”)  
end  
end

The above example will return something like this:  
[{:name =\> “A”}, {:name =\> “B”}]

---

<div class="post-metadata">

**Author:** ![xxmithila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/ebca7d/32.png) [@xxmithila](https://www.ruby-forum.com/u/xxmithila)\
**Post date:** [October 29, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/sql-in-rails/152051/3 "2008-10-29T17:49:40Z")

</div>

thankx Andrew  
I’ll try it
