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:
- a MongoDB database in order to store images (probably with
mongoidgem). - a Neo4j database (probably with
neo4j-railsgem) instead of PostgreSQL for some tables.
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.
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.
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:
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.