# Ruby application for sql statements

**URL:** <https://www.ruby-forum.com/t/ruby-application-for-sql-statements/196661>\
**Category:** Rails\
**Created:** [May 1, 2008, 9:56am UTC](https://www.ruby-forum.com/t/ruby-application-for-sql-statements/196661 "2008-05-01T09:56:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![StudxXxent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@StudxXxent](https://www.ruby-forum.com/u/StudxXxent)\
**Post date:** [May 1, 2008, 9:56am UTC](https://www.ruby-forum.com/t/ruby-application-for-sql-statements/196661/1 "2008-05-01T09:56:35Z")

</div>

Hello 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 (scaffolds have  
been generated);  
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 still needs to be done is I currently have sql  
statements/queries in which I am using to mine data from my database  
(eg. “find minimum time someone has been employed”  
Select min(time) from table…etc)

What I need to do is write a ruby application (one for each sql  
statement), where the sql statement is hardcoded into the ruby code.  
I need the ruby code to:

1. connect to the oracle database
2. run the sql statement on the oracle database
3. take the results and return them in an array or  
something of that nature (so that they can be used in an html file for  
output on the web application)

If someone could give me a skeleton of how this should look in ruby or  
give me example code I would forever be in your debt.  
Thanks

---

<div class="post-metadata">

**Author:** ![StudxXxent](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@StudxXxent](https://www.ruby-forum.com/u/StudxXxent)\
**Post date:** [May 1, 2008, 10:19am UTC](https://www.ruby-forum.com/t/ruby-application-for-sql-statements/196661/2 "2008-05-01T10:19:04Z")

</div>

StudxXxent wrote:

> What I need to do is write a ruby application (one for each sql  
> statement), where the sql statement is hardcoded into the ruby code.  
> I need the ruby code to:
> 
> 1. connect to the oracle database
> 2. run the sql statement on the oracle database
> 3. take the results and return them in an array or  
> something of that nature (so that they can be used in an html file for  
> output on the web application)

You need to learn about ActiveRecord. This is too large a subject for a  
quick answer on a forum.

Start with Rails tutorials. Take the time to understand how they are  
using ActiveRecord, how the models relate to database tables and how the  
#find method works for extracting data, including uses of #has\_many,  
#belongs\_to and #has\_one for linking models and use of :includes in the  
#find method for doing implicit joins on tables.

Search for information about ActiveRecord to take you further. It  
sounds like you are trying to fit Rails to an existing database schema,  
so google for “rails activerecord legacy schema” and you should get a  
few sites with advice on ways to access existing schemas from rails.

The ActiveRecord reference is available starting here:  
[http://api.rubyonrails.com/classes/ActiveRecord/Base.html](http://api.rubyonrails.com/classes/ActiveRecord/Base.html)  
however this is a reference and probably will only be useful as such  
alongside tutorials and other material.
