Mar*_* S. 1 ruby-on-rails mongodb rails-migrations mongoid ruby-on-rails-3
我目前正在开发一个rails应用程序,我们在后端使用mongoid/mongoDB.我知道我不需要像迁移这样的ActiveRecord来迁移模式,但我需要在更改mongoid模型定义时迁移数据.有没有其他人在那里遇到同样的情况,如果是这样你怎么处理它?
即使您没有进行架构更改,也可能需要在字段之间移动数据,或者删除代码库中不再使用的字段.在部署新代码时可以运行迁移,这很好.我建议使用一个叫做的宝石mongoid_rails_migrations.这为您提供了您习惯的迁移生成器,并为迁移数据提供了一些组织.
class MyMigration < Mongoid::Migration
def self.up
MyModel.all.each do |model|
# label was renamed to name
model.set :name, model[:label] # copy the data from the old field to the new one
model.remove_attribute :label # remove the old field from the document
model.save!
end
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5310 次 |
| 最近记录: |