简体   繁体   English

Rails 3:将 model 迁移到 MongoDB

[英]Rails 3: Migrating a model to MongoDB

I have a few models in my existing Rails 3 app that are completely non-relational to the other models in the application's ecosystem.我现有的 Rails 3 应用程序中有一些模型与应用程序生态系统中的其他模型完全无关。 The underlying data is incredibly large and it would benefit in being moved from my relational db (MySQL) to a non-relational document data store, such as MongoDB.基础数据非常大,从我的关系数据库(MySQL)转移到非关系文档数据存储(例如 MongoDB)会受益。 The problem I currently have is that the model is instantiated in several places in my code base using Foo.create, Foo.new etc. My question(s) are the following:我目前遇到的问题是 model 使用 Foo.create、Foo.new 等在我的代码库中的多个位置实例化。我的问题如下:

  • What is the ideal way for me to use MongoDB and MySQL side by side in a Rails 3 app?我在 Rails 3 应用程序中并排使用 MongoDB 和 MySQL 的理想方式是什么?

  • Should I use a 'ghost' method on ActiveRecord.create for the model I want to migrate to MongoDB and write the logic there that persists it to a different data store?我应该在 ActiveRecord.create 上为 model 使用“幽灵”方法吗? This sounds very brittle, but it's just a thought.这听起来很脆弱,但这只是一个想法。

1) Just do it, using mongoid or mongo_mapper. 1) 使用 mongoid 或 mongo_mapper 即可。 Both are great!两者都很棒!

2 ) NO! 2)不! Replace ActiveRecord superclass with that of mongoid or mongo_mapper.将 ActiveRecord超类替换为 mongoid 或 mongo_mapper 的超类。

The only difficult thing about polyglot persistence is to remember the word, polyglot.关于多语言持久性的唯一困难是记住这个词,多语言。 Don't over-complicate.不要过于复杂。 The models that inherit ActiveRecord are mapped to the SQL-db, the models that don't inherit it, are not.继承 ActiveRecord 的模型映射到 SQL-db,不继承它的模型则不是。 Simple as that.就那么简单。 Same with the models that inherit the Mongo-ORM-class, only they are mapped to MongoDB.与继承 Mongo-ORM 类的模型相同,只是它们映射到 MongoDB。

I use postgreSQL alongside MongoDB (mongo_mapper).我使用 postgreSQL 和 MongoDB (mongo_mapper)。 One awesome thing for everybody to try out, is storing errors in Mongo docs.每个人都可以尝试的一件很棒的事情是在 Mongo 文档中存储错误。 Beats *.log files.击败 *.log 文件。

Final note: Having Foo.create and Foo.new at multiple places is a code smell and you should probably refactor according to DRY and SRL.最后一点:在多个地方使用 Foo.create 和 Foo.new 是一种代码味道,您可能应该根据 DRY 和 SRL 进行重构。 Just a friendly note:)只是一个友好的提示:)

There's no inherent problem in having some classes in ActiveRecord and some in Mongoid, just transfer the data over. ActiveRecord 中的某些类和 Mongoid 中的某些类没有固有问题,只需将数据传输过来即可。

This is a rather simplistic example, but below you would still be able to say @account.transactions这是一个相当简单的例子,但在下面你仍然可以说@account.transactions

class Account < ActiveRecord::Base

  def transactions
    Transaction.where(:account_id=>self.id).all
  end

end

class Transaction
  include Mongoid::Document
  field :account_id, :type=>Integer

  def account
    Account.find(account_id)
  end
end

Look at mongoid , an orm for mongodb.查看mongoid , mongodb 的 orm。 It uses ActiveModel and can essential behave just like your ActiveRecord based models.它使用 ActiveModel 并且可以像基于 ActiveRecord 的模型一样运行。 Once you include the mongoid document class in your model, add the fields and config the connection it should be fairly transparent.在 model 中包含 mongoid 文档 class 后,添加字段并配置连接,它应该是相当透明的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM