# Use Mysql.new() without DB data

**URL:** <https://www.ruby-forum.com/t/use-mysql-new-without-db-data/89778>\
**Category:** Rails\
**Created:** [March 22, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/use-mysql-new-without-db-data/89778 "2007-03-22T18:41:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 22, 2007, 6:41pm UTC](https://www.ruby-forum.com/t/use-mysql-new-without-db-data/89778/1 "2007-03-22T18:41:35Z")

</div>

How can I use MySQL.new() inside one of my models without specifying  
the specifics of the database connection?

So in one of my models I have something like:  
m = Mysql.new(“localhost”,“myuser”,“mypass”,“mydb”)  
r = m.query("SELECT ")

How can I have that SQL statement line above use the global database  
connection that I already have available to ActiveRecord?

I know another option is find\_by\_sql, like:  
@somemodels = Somemodel.find\_by\_sql("SELECT ")

But I am not fetching objects with my SQL. According to the API, the  
left-hand side of that statement must be model objects.

Am I right on this? Is there a way to use the global db settings  
while using m = Mysql.new() so I don’t have to write the user/pass/db  
attributes inside the model?

Thanks,  
Rob

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 22, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/use-mysql-new-without-db-data/89778/2 "2007-03-22T21:50:29Z")

</div>

You can use ActiveRecord::Base.connection, e.g.  
ActiveRecord::Base.connection.select\_all(“some nasty sql here”); and  
it will return as an array of hashes.

-Shawn

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 22, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/use-mysql-new-without-db-data/89778/3 "2007-03-22T22:49:43Z")

</div>

Bingo. Exactly what I was needing.

Thanks Shawn,  
Rob
