0

I perodicially need to access a mysql database, my primary data store is mongo, which I access with mongoid. I want to know the best way to manage connections to mysql (with the mysql2 gem - 0.2.7) without using active record.

I current do the following ...

# In config/initializers/mysql.rb
class MySqlConnection

  def self.client
    @client ||= Mysql2::Client.new(host: ENV['mysql_host'], 
                                   username: ENV['mysql_username'],
                                   password: ENV['mysql_password'], 
                                   database: ENV['mysql_database'])
  end

end

and then I use connection, like so ...

rows_q = "SELECT * FROM amaizng_table WHERE great_column = '#{cool_value}' "
rows = ::MySqlConnection.client.query(rows_q)

And everything is working okay -- but I have a sneaking suspicion that I am doing something horribly wrong, and things are going to explode down the road.

Also Note, the application is hosted on heroku

Anyone know the best way to approach this?

Thanks!

Jonathan

Jonathan
  • 16,077
  • 12
  • 67
  • 106

1 Answers1

0

why, just WHY would you get rid of ActiveRecord's awesomeness (or any other ORM, really) ?

class Amazing < ActiveRecord::Base
  establish_connection :mysql_database
end

so simple it hurts. See this for more details.

m_x
  • 12,357
  • 7
  • 46
  • 60
  • Too me it seems like overkill for two queries -- but maybe its the best solution – Jonathan Nov 10 '12 at 12:52
  • All your queries will be wrapped up in transactions. You can put your credentials in database.yml, and make the file not readable for unauthorized users on your box, which is more secure. If you need to extend your model later, you're good to go. – m_x Nov 10 '12 at 12:56