Home arrow Ruby-on-Rails arrow Page 3 - 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 - Magic Multi-Connections
(Page 3 of 4 )

Dr Nic Williams's Magic Multi-Connections gem (http://magicmodels.rubyforge.org/ magic_multi_connections/) allows you to connect to different databases concurrently from the same application. This is very useful when using one master and several read-only slaves serving the same models. The syntax is transparent; it uses module namespaces and imports the models (ActiveRecord::Base subclasses) into the namespaces.

For a single-master situation, you could define another database connection in database.yml for the read slave:

  read_slave:
    adapter: postgresql
    database: read_only_production
    username: user
    password: pass
    host: read_slave_host

This database is backed by a module, which mirrors the ActiveRecord classes using this database connection:

  require 'magic_multi_connections'
  module ReadSlave
    establish_connection :read_slave
  end

Now, all pre-existing models can be accessed through the read_slave connection by prefixing the model class with ReadSlave::.

  # use the read-only connection
  @user = ReadSlave::User.find(params[:id])

  # write to the master (can't use @user.update_attributes because it would
  # try to write to the read slave)
  User.update(@user.id, :login => "new_login")


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 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials