Thu
16
Mar '06
|
|
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]
You can require all of your models this way:
Dir["app/models/**/*.rb"].sort.each { |file| require file }
(Actually, you can leave off the “sort” method, of course)
Neat trick! I didn’t know about Dir[...], thanks Scott.