← all posts

17 October 2009

Connecting to Multiple Databases Using ActiveRecord

You can call establish_connection with the key that points to another database config in your config/database.yml

class Person < ActiveRecord::Base
  establish_connection(:hr)
end

class Ticket < ActiveRecord::Base
  establish_connection(:bug_tracker)
end

If you have a whole bunch of models that need to connect to another database:

class HR < ActiveRecord::Base
  establish_connection(:hr)
end

class People < HR
  # ...
end

class Resource < HR
  # ...
end

— Ryan

Got a thought? Email me or find me on Mastodon.

Before this: Fixed By Rails 3

After this: Size != Count