Home arrow Ruby-on-Rails arrow Page 2 - More Advanced Database Features and Rails
RUBY-ON-RAILS

More Advanced Database Features and Rails


In this fourth part of a five-part series on databases and Ruby-on-Rails, you will learn how and when to handle triggers, rules, and stored procedures; how to connect to multiple databases; and more. This article is excerpted from chapter four of the book Advanced Rails, written by Brad Ediger (O'Reilly; ISBN: 0596510322).Copyright © 2008 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
February 03, 2010
TABLE OF CONTENTS:
  1. · More Advanced Database Features and Rails
  2. · Connecting to Multiple Databases
  3. · Magic Multi-Connections
  4. · Caching

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
More Advanced Database Features and Rails - Connecting to Multiple Databases
(Page 2 of 4 )

Occasionally, you will have the need to connect to several different databases from one application. This is useful for migrating from an old schema to a new one. It is also helpful if you have differing data requirements within one application; perhaps some data is more critical and is stored on a high-availability database cluster. In any case, it is easy in Rails. First, specify multiple database environments in the database.yml configuration file:

  legacy:
    adapter: mysql
    database: my_db
    username: user
    password: pass
    host: legacy_host

  new:
    adapter: mysql
    database: my_db
    username: user
    password: pass
    host: new_host

Then, you can simply refer to these configuration blocks from the ActiveRecord class definition using the ActiveRecord::Base.establish_connection method:

  class LegacyClient < ActiveRecord::Base
    establish_connection "legacy"
  end

  class Client < ActiveRecord::Base
    establish_connection "new"
  end

This approach also works with multiple Rails environments. Just specify each environment in the database.yml file as usual:

  legacy_development:
    # ...

  legacy_test:
    # ...

  legacy_production:
    # ...

  new_development:
    # ...

  new_test:
    # ...

  new_production:
    # ..
.

Then, use the RAILS_ENV constant in the database configuration block name:

  class LegacyClient < ActiveRecord::Base
    stablish_connection "legacy_#{RAILS_ENV}"
  end

  class Client < ActiveRecord::Base
    establish_connection "new_#{RAILS_ENV}"
  end

You can go one step further and DRY this code up by using class inheritance to define which database an ActiveRecord class belongs to:

  class LegacyDb < ActiveRecord::Base
    self.abstract_class = true
    establish_connection "legacy_#{RAILS_ENV}"
 
end

  class NewDb < ActiveRecord::Base
    self.abstract_class = true
    establish_connection "new_#{RAILS_ENV}"
  end

  class LegacyClient < LegacyDb
  end

  class Client < NewDb
  end

The self.abstract_class = true statements tell ActiveRecord that the LegacyDb and NewDb classes cannot be instantiated themselves; since they represent database connections, they are not backed by concrete tables in the database.


blog comments powered by Disqus
RUBY-ON-RAILS ARTICLES

- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form
- Ruby on Rails Templates and Layouts
- Action Pack Controller Creation
- Writing an Action Pack Controller

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials