How to Configure Mongomapper and Activerecord in Same Ruby Rails Project

How to configure MongoMapper and ActiveRecord in same Ruby Rails Project

Include your mongo_mapper gem in you Gemfile. Then in the models that you slowly want to start migrating over to MongoMapper, you just include this in your model:

include MongoMapper::Document

here is an example of a Mongo publisher model

class Publisher
include MongoMapper::Document

key :_id, String
key :mtd_uniques, Integer
key :mtd_demo_uniques, Integer
key :archive, Array
key :ignore, Boolean
end

My user model (postgres):

class User < ActiveRecord::Base
validates_presence_of :first_name, :last_name, :email, :type
acts_as_authentic

def self.inherited(child)
child.instance_eval do
def model_name
User.model_name
end
end
super
end
end

The nice thing about this is that all of your other models still use ActiveRecord so you can use 2 different databases till everything is migrated over to Mongo. This is an example from what I'm using. Large data aggregations using MongoMapper, and User model using postgres (app hosted on Heroku)

For my setup I dumped config stuff in my config.yml

development:
adapter: MongoDB
host: localhost
database: my-dev-db

test:
adapter: MongoDB
host: localhost
database: my-dev-db

staging:
adapter: MongoDB
host: remote-host (for me amazon ec2)
database: my-staging-db

production:
adapter: MongoDB
host: remote-host (for me amazon ec2)
database: my-production-db

and created an initializer that differentiates between the 2 DBs:

/initializers/database.rb

# load YAML and connect
database_yaml = YAML::load(File.read("#{Rails.root}/config/config.yml"))
puts "Initializing mongodb"
if database_yaml[Rails.env] && database_yaml[Rails.env]['adapter'] == 'MongoDB'
mongo_database = database_yaml[Rails.env]
MongoMapper.connection = Mongo::Connection.new(mongo_database['host'], 27017, :pool_size => 5, :timeout => 5)
MongoMapper.database = mongo_database['database']
end

Configure/Setup a Rails App to use both Mongodb and Mysql together

You can use both ActiveRecord and MongoMapper in a project, but if you want associations between records, you'll have to code the methods yourself (which is not that hard for the basic cases).

See http://mongomapper.com/documentation/getting-started/rails.html for setting up MongoMapper and Rails.

You might look also into DataMapper, which supports multiple backends. I'm not sure if it can do multiple different types of DB's in one project though. See http://datamapper.org/

MongoMapper and migrations

Check out Mongrations... I just finished reading about it and it looks like what you're after.

http://terrbear.org/?p=249

http://github.com/terrbear/mongrations

Cheers! Kapslok

Rails 3 Polymorphic Association between one MongoMapper model and one/many Active Record model/s

Yes this can be done.

To create a polymorphic association you need both the class and an id. Idiomatically the fields will be named <assoc>_type and <assoc>_id. You will need to do some wiring up to make everything work.

  1. Create a MongoMapper::Document Class with the keys <assoc>_type and <assoc>_id with the correct types (I believe MongoMapper allows Class as a key type) along with any other keys you may need.
  2. Define the method <assoc> and <assoc>=

    def assoc
    assoc_type.find(assoc_id)
    end

    def assoc=(input)
    assoc_type = input.class #STI makes this more complicated we must store the base class
    asspc_id = input.id
    end
  3. Possibly add a method to your ActiveRecord models allowing them to access you MongoMapper logging class. If there are a lot, you may want to build a module and include it in all the classes that need that kind of functionality.

‡ replace with something meaningful for you application like 'reference' or 'subject'

Setting up mongomapper for multiple databases

The source link you posted is what you want. Those methods are all mixed-in to your models, so:

class MyModel
include MongoMapper::Document
connection(Mongo::Connection.new('localhost', 27017))
set_database_name "my_database"
# ...
end

Since it's just a Mongo::Connection, you can use any of its supported options. (source)

rails 4 with mongoid or mongomapper ? which is perfect for own product

Mongoid has been officially incorporated under the MongoDB umbrella. It is a more mature product and it's actively developed, compared with MongoMapper.

I'd definitely not use MongoMapper for a new project.

Running both MongoDB and Mysql on Rails 2.3.6

So you would use active record and standard database.yml file. You would also run rails g mongo_mapper:config to create your mongo.yml file. These are seperate and should allow you to use both in your model. This would work for mongoid too.

Using MongoDB with Ruby On Rails and the Mongomapper plugin

No you don't have to fetch all the guestbook entries if you fetch a user, this is the mongo query for and MongoMapper shouldn't work so much differently (For myself, I'm using Mongoid):

db.users.find({_id: '21314'}, {guestbook: 0})
// instead of {guestbook: 1} which would return only the guestbook

Something to be aware of, MongoDB still has a limit of 4mb for a single document but that should be tens of thousands of guestbook entries. You might as well push older ones into a sort of archive.



Related Topics



Leave a reply



Submit