gemfileにmongo_mapper gemを含めます。そして、あなたはゆっくりMongoMapperにオーバー移行を開始するモデルで、あなたは自分のモデルでこれを含める:ここ
include MongoMapper::Document
はMongoの出版社のモデルの一例である
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の):
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
このことについての素晴らしい事はすべてがモンゴにオーバー移行するまで、あなたが2つの異なるデータベースを使用できるように、他のモデルのすべてが、まだのActiveRecordを使用することです。これは私が使っているものの例です。私のセットアップについてはpostgres(Herokuの上でホストされているアプリ)
を用いた大MongoMapperを使用して、データの集計、およびUserモデルはI私の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
に設定のものをダンプし、2つのDB区別初期化子を作成しました:
/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
設定ファイル –
ありがとう@Chris。 'File.read("#{Rails.root} /config/config.yml ")'その後、それがうまくいった。これに答えて、それをはっきりさせる時間をとってくれてありがとう。 –
ああそう! RAILS_ROOTを定数として設定しました。それがうまくいってうれしい。 –