How can I use Mongoid and ActiveRecord in parallel in Rails 3? How can I use Mongoid and ActiveRecord in parallel in Rails 3? mongodb mongodb

How can I use Mongoid and ActiveRecord in parallel in Rails 3?


What I did was just mock the relationship with methods in each the AR model and the Mongoid model like so.

# visit_session.rbclass VisitSession  include Mongoid::Document  include Mongoid::Timestamps  field :user_id, type: Integer  index({user_id: 1},{name: :user_id_index})  # Mock a belongs_to relationship with User model  def user    User.find(self.user_id)  endend# user.rbclass User < ActiveRecord::Base  # Mock a has_many relationship with VisitSession Mongoid model  def visit_sessions    VisitSession.where(user_id: self.id)  endend

Of course you won't have all the AR methods on VisitSession Mongoid model but you'll at least be able to mock the relationship between the two fairly well.

Hope this helps.


I don't see any reason why you couldn't have both ActiveRecord and Mongoid models in the same application. That being said, I'm almost certain that you'll run into issues if you try to create relationships between your ActiveRecord and Mongoid models.

If your ActiveRecord models are heavily inter-related, but better suited to a document structure, then I would suggest just biting the bullet and converting them all to Mongoid documents. I had to do this recently on a (large-ish) project, and it's significantly less stressful than you would think.

If you have good unit tests for your models, then it should be a total snap. If you don't - write your unit tests first, make sure they pass with ActiveRecord and then start migrating things over to Mongoid.


i created a module for spoofing the relation in active record models.

module MongoRelations  def belongs_to_mongo(name, options = {})    id_name = "mongo_#{name}_id".to_sym    mongo_model = options[:through] || "Mongo::#{name.to_s.camelize}".constantize    define_method(name) do      id = send(id_name)      mongo_model.find(id) if id.present?    end    define_method("#{name}=") do |value|      send("#{id_name}=".to_sym, value.try(:id).to_s)    end  endend

In my SQL table, I name my mongo relations using the convention mongo_XXX_id, instead of XXX_id

I also namespace all my mongo models under Mongo::

in my active record model

class Foo < ActiveRecord::Base    belongs_to_mongo :XXXend

which allows

Foo.new.XXX = Mongo.find('123')Foo.XXX

or

Foo.new.XXX_id = '123'Foo.XXX