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.
- Create a MongoMapper::Document Class with the keys
<assoc>_type
and<assoc>_id
with the correct types (I believe MongoMapper allowsClass
as a key type) along with any other keys you may need. 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
endPossibly 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
What Is a Worker in Ruby/Rails
How to Force One Field in Ruby's CSV Output to Be Wrapped with Double-Quotes
How to Find a Model's Relationships
Using the Value of a Variable as Another Variables Name in Ruby
Heroku: How to Push Seeds.Rb to Existing Rails App
Spork 0.9.2 and Rspec 3.0.0 = Uninitialized Constant Rspec::Core::Commandline (Nameerror)
Vi Input Mode in Command Line Matlab
Differencebetween "Be_True" and "Be True" in Rspec
Rails: How to Get a File Extension/Postfix Based on the Mime Type
Ruby Equivalent of Perl Data::Dumper
How to Instruct Capistrano 3 to Load My Shell Environment Variables Set at Remote Host
Escape Double and Single Backslashes in a String in Ruby
What Happens When Modifying Gemfile.Lock Directly
Verb-Agnostic Matching in Sinatra
How to Run Ruby 2.0 with Jruby 1.7