Ruby‎ > ‎Databases‎ > ‎

ActiveRecord

Although you could easily configure the database in every worker, configuring it on a global basis is more convenient. An added bonus is this approach will also create the db connection for you.

For Rails 3

Just add a single line to your SimpleWorker config:

SimpleWorker.configure do |config|
  # Gem v2.X
config.project_id = PROJECT_ID
config.token = TOKEN_ID

# Gem v1.X
#config.access_key = ACCESS_KEY #config.secret_key = SECRET_KEY

 config.database = Rails.configuration.database_configuration[Rails.env] # ADDED THIS LINE end

Outside of Rails or previous versions of Rails

Outside of rails, just pass in a hash to config.database in your SimpleWorker config block:

SimpleWorker.configure do |config|
# Gem v2.X
config.project_id = PROJECT_ID
config.token = TOKEN_ID

# Gem v1.X
 #config.access_key = ACCESS_KEY #config.secret_key = SECRET_KEY # Here's the new database config config.database = { :adapter => "mysql2", :host => "host_url", # Not localhost* :database => "appdb", :username => "appuser", :password => "secret" } end

Now just use your models like you would in your Rails application using ActiveRecord methods.

* Note: Since SimpleWorker is running on the cloud and not directly on your application server, you can't connect to your database using localhost (127.0.0.1). Your database must be externally accessible and you'll need to provide the URL so that your workers can access your database from our servers.

Comments