Multiple Rails ORM

We have a Rails 3 application with a PostgreSQL database (with ~ 10 tables) being displayed activerecord. Everything is working fine.

However, we would also like to use:

  • a MongoDB database for storing images (possibly with mongoidgem).
  • a Neo4j (possibly with neo4j-railsgem) instead of PostgreSQL for some tables.

Using a database with one Rails ORM is simple, thanks database.yml. But when there is more than one ORM, how can we process it? Is there a good way to do this? For example, ActiveHash (and ActiveYaml) may work well with ActiveRecord. I think it might be possible to allow different ORMs to work together. Thanks for any advice.

+5
source share
2 answers

It really depends on the type of ORM. A great way to do this is to use inheritance. For example, you can have several databases and adapters defined in the database.yml file. You can easily talk to them using the ActiveRecord install_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 downside here is that when you connect to multiple active databases, migration can be a bit risky.

Mix ORM

Mixing ORMS is very simple. for example, mongodb (with mongoid) simply does not inherit from the 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

ORM, , . , mongoid ActiveRecord, , , .

+4

-, ORM . , Mongoid "" ActiveRecord - . (. )... .

, - , , . MongoDB ? , Mongoid - ORM (, , ODM)? ORM/ODM, ? .

, " " ( ), : https://github.com/jwood/tenacity. , .

+1

All Articles