# Ruby program for sql query

**URL:** <https://www.ruby-forum.com/t/ruby-program-for-sql-query/136357>\
**Category:** Ruby\
**Created:** [May 1, 2008, 5:37am UTC](https://www.ruby-forum.com/t/ruby-program-for-sql-query/136357 "2008-05-01T05:37:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [May 1, 2008, 5:37am UTC](https://www.ruby-forum.com/t/ruby-program-for-sql-query/136357/1 "2008-05-01T05:37:00Z")

</div>

Hi everybody,

I am currently working on a database system that utilizes ruby on rails  
with a oracle database. Here’s the situation:  
the ruby on rails web application is up and running;  
my oracle database is full of tables that are populated with data;  
there is a successful connection between rails and my oracle database;  
however, what isn’t finished is I currently have data mining queries in  
sql (eg. find minimum time someone has been employed = “Select  
min(time) from table…etc”) and what I need to do is write one ruby  
program for each of my sql queries so that the ruby program:

1. connects to the oracle database each time the query is called
2. runs the sql query on the oracle database
3. takes the results from the query and returns them in an array or  
something of that nature (so that I can use the results in an html file  
on the ruby on rails web application).

I have some experience writing ruby, although it’s been a couple years,  
but if someone could give me example code of what this would look like  
(using different sql queries) I would really appreciate that. I’m  
keeping my fingers crossed,  
Thanks

---

<div class="post-metadata">

**Author:** ![ron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/ecae2f/32.png) [@ron](https://www.ruby-forum.com/u/ron)\
**Post date:** [May 1, 2008, 6:39am UTC](https://www.ruby-forum.com/t/ruby-program-for-sql-query/136357/2 "2008-05-01T06:39:27Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Ronald Reichard wrote:  
| Hi everybody,  
|  
| I am currently working on a database system that utilizes ruby on rails  
| with a oracle database. Here’s the situation:  
| the ruby on rails web application is up and running;  
| my oracle database is full of tables that are populated with data;  
| there is a successful connection between rails and my oracle database;  
| however, what isn’t finished is I currently have data mining queries in  
| sql (eg. find minimum time someone has been employed = “Select  
| min(time) from table…etc”) and what I need to do is write one ruby  
| program for each of my sql queries so that the ruby program:  
| 1) connects to the oracle database each time the query is called  
| 2) runs the sql query on the oracle database  
| 3) takes the results from the query and returns them in an array or  
| something of that nature (so that I can use the results in an html file  
| on the ruby on rails web application).  
|  
| I have some experience writing ruby, although it’s been a couple years,  
| but if someone could give me example code of what this would look like  
| (using different sql queries) I would really appreciate that. I’m  
| keeping my fingers crossed,  
| Thanks

ActiveRecord comes with a #find and #find\_by\_sql method:  
[http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000344](http://ar.rubyonrails.org/classes/ActiveRecord/Base.html#M000344)

All you need to do is generate the appropriate controllers and views for  
these controllers to display the data you need.

Also, it is better to ask the Rails community questions regarding Rails:

> **[Community](https://rubyonrails.org/community)**
>
> A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ Calvin: Sometimes when I’m talking, my words can’t keep up with my  
thoughts. I wonder why we think faster than we speak.  
~ Hobbes: Probably so we can think twice.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgZSWgACgkQbtAgaoJTgL/MFQCeKPZew2bSZYD/A4+j0vH0Qeya  
R9YAoJivjqG2zXLJnWpzHYQMtF6fPLiM  
=D3gv  
-----END PGP SIGNATURE-----
