T5i
T5i

Reputation: 1530

Multiple Rails ORM

We have a Rails 3 application with a PostgreSQL database (with ~10 tables) mapped by activerecord. Everything's working fine.

However, we could also like to use:

Using a database with one Rails ORM is simple, thanks to database.yml. But when there's more than one ORM, how can we process? Is there a good way to do so? For instance, ActiveHash (and ActiveYaml) can work well with ActiveRecord. I think there could be a possibility to let differents ORM working together. Thanks for any tips.

Upvotes: 5

Views: 1303

Answers (3)

Roberto Furtado
Roberto Furtado

Reputation: 43

Well, I had the same problem today using neo4j gem. I added require 'active_graph/railtie' in my application.rb.

So, when I want generate a model with ActiveGraph I use: rails generate model Mymodel --orm active_graph, with --orm option you can specify an orm to use.

Without --orm option, it will use AR, by default.

Upvotes: 1

Raphael
Raphael

Reputation: 1721

First off, I strongly recommend you do not try to have multiple ORMs in the same app. Inevitably you'll want your Mongoid object to 'relate' to your ActiveRecord object in some way. And there are ways (see below)...but all of them eventually lead to pain.

You're probably doing something wrong if you think you 'need' to do this. Why do you need MongoDB to store images? And if you're using it just as an image store, why would you need Mongoid or some other ORM (or more accurately, ODM)? If you really, really need to add a second data store and a second ORM/ODM, can you spin it off as a separate app and call it as a service from your first one? Think hard about this.

That said, if you really want to go with "polyglot persistence" (not my term), there is a decent gem: https://github.com/jwood/tenacity. It's no longer actively developed, but the maintainer does fix bugs and quickly responds to inquiries and pull requests.

Upvotes: 0

Jason Waldrip
Jason Waldrip

Reputation: 5148

This really depends on the type of ORM. A great way to do this is by using inheritance. For example you can have multiple databases and adapters defined in your database.yml file. You can easily talk to these using the ActiveRecord establish_connection method.

# A typical Active record class
class Account < ActiveRecord::Base
  ...
end

# A new database connection
class NewConnection < ActiveRecord::Base
  self.abstract_class = true
  establish_connection "users_database"
end

# A new Active record class using the new connection
class User < NewConnection
  ...
end

The only down side here is that when you are connection to multiple active record databases migrations can get a little bit dicey.

Mixing ORM's

Mixing ORMS is easy. for example mongodb (with mongoid), simply dont inherit from active record and include the following in the model you want to use mongo:

class Vehicle
  include Mongoid::Document

  field :type
  field :name

  has_many :drivers
  belongs_to :account

end

ORMs built on top of active model play very nicely together. For example with mongoid you should be able to define relations to ActiveRecord models, this means you can not only have multiple databases but they can easy communicate via active model.

Upvotes: 6

Related Questions