Can I call MySql statements directly?

Hi All.

I have a mysql statement that I would really really like to call from my
Ruby program which goes like this:

SELECT a, b, DAYOFWEEK(date_time) as DOW,
HOUR(date_time) at hr,
AVG(x/y)
FROM records;

This is possible by creating a 3-dimentional array of a, b, date_time
containing x/y, and then finding averages and putting it into a
4-dimensional array of a, b, dow, and hr.

3-dim_array
a
b
date_time

4-dim_array
a
b
dow
hr

My reasons for not wanting to do it this way is that there could be
100,000+ records. Also, look at how pretty and compact that the MySql
statement is! I would much rather let MySql handle all that because
arrays will get very big, and I’m not sure if or how Ruby can handle
that.

Can someone tell me if I can use the above MySql statement to generate
the 4-dimentional array? If not, can someone please help me find a
possible solution?

Danke in advance!

You can always use the raw connection.

ActiveRecord::Base.connection.execute

See here for more related methods:

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/DatabaseStatements.html#M000625

-Jonathan.

Search the API for find_by_sql

It’ll let you generate arbitrary SQL statements and get the results
back via ActiveRecord

Regards

Dave M.

Whoa. This is all very very useful! Thanks, guys.

-Lisa