to configure your "config/database.yml" to make their connections, for example, we have this file:# SQLite version 3.x# gem install sqlite3## Ensure the SQLite 3 gem is defined in your Gemfile# gem 'sqlite3'development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000# Warning: The database defined as "test" will be erased and# re-generated from your development database when you run "rake".# Do not set this db to the same as development or production.test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000This is the default setting with a base in SQLite, by default, configured with the name of the execution environments. Now we build a new configuration, I called it "external_database_1" note that we will connect to a MySQL database, which is perfectly possible as the hard work of how to deal with each type of database is carry by ActiveRecord. Also remember to reference the adapters GENs you are using, in this example "gem 'sqlite3 '; gem 'mysql2 '; "development: adapter: sqlite3 database: db/development.sqlite3 pool: 5 timeout: 5000test: adapter: sqlite3 database: db/test.sqlite3 pool: 5 timeout: 5000production: adapter: sqlite3 database: db/production.sqlite3 pool: 5 timeout: 5000external_database_1: adapter: mysql2 encoding: utf8 database: test_db pool: 5 username: root password: root host: localhostStep 2: Create a class to handle the connectionThe second step is to create a class that served to manipulate the new connections, let's first create a file called "my_databases.rb" inside of folder "config/initializers/", all we need now is to create a class that inherits ActiveRecord::Base, for example:class ProjectDB < ActiveRecord::Base self.abstract_class = true establish_connection Rails.env.to_symendclass TestDB < ActiveRecord::Base self.abstract_class = true establish_connection :external_database_1endMuch attention at this point!! Why two class?!Basically for organization and simplicity, so that each model be explicitly tied to which connection it belongs!The first class "ProjectdDB" refers connections standards (:development, :test, :production) in SQLite. Note the value that is passed to the method "establish_connection" which is Rails.env.to_sym.The second class "TestDB" refers to our external database in MySQLObs:. "self.abstract_class = true" means that the class is abstract, it can not be instantiated only inherited.At this point we already have all our application configured to work with different database. Now I will show by example how we have to work with it. Example 1: Create the users table.rails generate scaffold user name:string email:string invoke active_record create db/migrate/20121127205353_create_users.rb create app/models/user.rb invoke test_unit create test/unit/user_test.rb create test/fixtures/users.yml invoke resource_route route resources :users invoke scaffold_controller create app/controllers/users_controller.rb invoke erb create app/views/users create app/views/users/index.html.erb create app/views/users/edit.html.erb create app/views/users/show.html.erb create app/views/users/new.html.erb create app/views/users/_form.html.erb invoke test_unit create test/functional/users_controller_test.rb invoke helper create app/helpers/users_helper.rb invoke test_unit create test/unit/helpers/users_helper_test.rb invoke assets invoke coffee create app/assets/javascripts/users.js.coffee invoke scss create app/assets/stylesheets/users.css.scss invoke scss create app/assets/stylesheets/scaffolds.css.scssAs the users table should be in an external database, then:1o - Changing the file "app/models/user.rb"class User < ActiveRecord::Base attr_accessible :email, :nameendto: class User < TestDB attr_accessible :email, :nameendreplace ActiveRecord::Base by its subclass TestDB thus the application knows where users table will be created.2o - Run migration appropriatelyrake db:migrate RAILS_ENV=database_externa_1 == CreateUsers: migrating ==================================================== -- create_table(:users) -> 0.0711s == CreateUsers: migrated (0.0712s) ===========================================passing the connection "external_database_1" as a parameter, so the table will be