1

We have a few Rails 3 web sites that need to access a common database for order tracking and fulfillment.

Basically we want each site to have its own database and be able to access the common database as well.

I am thinking that creating a gem to access this second database is the way to go, but I am fairly new to Ruby and Rails.

Anyone done something like this before?

Any suggestions on how to implement this?

1

2 Answers 2

1

Try with something like:

# WARNING: untested code
module DatabaseA
  class Connection < ActiveRecord::Base
    self.abstract_class = true
    establish_connection :my_custom_connection
  end

  def const_missing(name)
    model = Class.new(Object.const_get(name))
    model.connection = Connection.connection
    const_set(name, model)
  end
end

Then you should use your models from this module:

DatabaseA::User.new
Sign up to request clarification or add additional context in comments.

Comments

1

I have written a gem to help with this: https://github.com/karledurante/secondbase

We use it in production now with rails 2 and rails 3 apps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.