If you find this useful, you might like my other articles, too.

This tutorial shows how to use distributed ruby to connect a rails application with another ruby service not running inside Rails.

Distributed Ruby (DRb)

I won’t waste time here explaining how to use DRb. Just a short example from Rubycentral with a very simple server:

and an even simpler client:

Active Record

As you want to connect the DRb Server to Rails, you probably want to use your model classes, too. Just include them (I’m still looking for a simple way to automagically include all models) and configure ActiveRecord:

Don’t repeat yourself!

Repeating the database configuration as I did in the above example is soooo against The Ruby Way. A better way is to parse config/database.yml and use the result to configure ActiveRecord. This has the added benefit of being able to use different database setups:

From Rails?

Just use the simple client from above inside your model or controller without special changes for Rails. Yes, it’s that simple. Enjoy!

Here is a short thread about problems with DRb.

DRb over unix sockets

Brian has a nice writeup how to use DRb over unix sockets – it’s extremly easy, just change the URI et voila…

[tags]ruby, rails, drb[/tags]

3 Responses to “Rails, ActiveRecord and Distributed Ruby (DRb) in a nutshell”

  1. 1

    You can require all of your models this way:

    Dir["app/models/**/*.rb"].sort.each { |file| require file }

    Scott Woods (December 7th, 2006 at 13:58)
  2. 2

    (Actually, you can leave off the “sort” method, of course)

    Scott Woods (December 7th, 2006 at 14:00)
  3. 3

    Neat trick! I didn’t know about Dir[...], thanks Scott.

    Frank Spychalski (December 10th, 2006 at 21:37)

Any comments? Or questions? Just leave a Reply:

Bad Behavior has blocked 881 access attempts in the last 7 days.