如何在同一个Ruby Rails项目中配置MongoMapper和ActiveRecord [英] How to configure MongoMapper and ActiveRecord in same Ruby Rails Project

查看:52
本文介绍了如何在同一个Ruby Rails项目中配置MongoMapper和ActiveRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的生产Ruby/Rails应用程序,我希望在时间允许的情况下,随着时间的推移将其迁移到MongoDB,因为不能一次全部重写它.我很希望能够在淘汰旧班级时就弃用我的旧班级.我计划使用MongoMapper.我找不到一个示例,其中没有人解释如何设置数据库配置文件以允许连接到一个应用程序中的两个数据存储.

I've got an existing production Ruby/Rails app that I want to migrate to MongoDB over time, as time permits, because it's not an option to just rewrite it all at one time. I'd love to be able to just deprecate my old classes as I get to them. I plan to use MongoMapper. I can't find an example where anyone explains how to set up the database config files to allow connection to both data stores within one app.

FWIW,我正在使用Rails3.感谢您的帮助.

FWIW, I'm using Rails 3. I appreciate the help.

推荐答案

在您的Gemfile中包含mongo_mapper宝石.然后,在您要慢慢开始迁移到MongoMapper的模型中,只需将其包括在模型中:

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

这是Mongo发布者模型的示例

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

我的用户模型(postgres):

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

有趣的是,您的所有其他模型仍然使用ActiveRecord,因此您可以使用2个不同的数据库,直到所有内容都迁移到Mongo.这是我正在使用的示例.使用MongoMapper进行大数据聚合,使用postgres(在Heroku上托管的应用)进行用户模型

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)

对于我的设置,我将配置内容转储到config.yml中

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

并创建了一个区分两个DB的初始化程序:

and created an initializer that differentiates between the 2 DBs:

/initializers/database.rb

/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

这篇关于如何在同一个Ruby Rails项目中配置MongoMapper和ActiveRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆