Forum: Ruby on Rails Use Mysql.new() without DB data

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7bf14ecf85afe06dd73c5a04506c5155?d=identicon&s=25 mazurr@gmail.com (Guest)
on 2007-03-22 18:41
(Received via mailing list)
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 <some complicated SQL joining......>")

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 <something
complicated......>")

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
7db798c17fea871c27402138b4497912?d=identicon&s=25 Shawn Roske (Guest)
on 2007-03-22 21:50
(Received via mailing list)
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
7bf14ecf85afe06dd73c5a04506c5155?d=identicon&s=25 mazurr@gmail.com (Guest)
on 2007-03-22 22:49
(Received via mailing list)
Bingo.  Exactly what I was needing.

Thanks Shawn,
Rob
This topic is locked and can not be replied to.