我怎样才能在Rails 3中使用Mongoid和ActiveRecord的并行? [英] How can I use Mongoid and ActiveRecord in parallel in Rails 3?

查看:88
本文介绍了我怎样才能在Rails 3中使用Mongoid和ActiveRecord的并行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是轨道3,开始了我与ActiveRecord的应用程序。现在,我有很多机型,而且关系也开始变得复杂,有的可能是pssed与面向文档的结构更简单的EX $ P $,所以我想尝试迁移到MongoDB中,并使用Mongoid。

I'm using rails 3, and began my application with ActiveRecord. Now, I have many models, and the relations are starting to get complicated, and some could be more simply expressed with a Document-Oriented structure, so I'd like to try migrating to MongoDB and use Mongoid.

我一直听说你不必eitheer使用MongoDB的全部或全无,但在迁移时,你可以使用两个并行。我不知道怎么走,虽然这个来自文档。

I've always heard that you didn't have to eitheer use all MongoDB or nothing, but that you could use the two in parallel while migrating. I don't see how to go about this from the docs though.

例如,我有:

class User < ActiveRecord::Base
   has_many :items
   has_many :products, :through => :items
end

class Product < ActiveRecord::Base
   has_many :items
end

class Item < ActiveRecord::Base
   belongs_to :user
   belongs_to :product

   # alot of data that fits a hierarchical document-oriented structure
end

我想通过理想与Mongoid文件替换我的项目ActiveRecord模型开始,所以我的项目都存储在MongoDB中,我的用户产品可以留在我的SQL数据库

I'd like to ideally begin by replacing my Item activerecord model with a Mongoid document, so my items are stored in MongoDB, and my Users and Products can stay in my SQL DB

事情是,我不知道如何做到这一点。我要对这个正确的方式?

Thing is, I don't see how to do this. Am I going about this the right way?

也许,另一种选择是保留基本AR项目

Perhaps another alternative is to keep a base AR Item

class Item < ActiveRecord::Base
   has_one :mongodb_item  ?? # I know this is wrong
end

class MongodbItem
   include Mongoid::Document
   belongs_to AR_Item ???    # I know this is also wrong
end

谢谢!

推荐答案

我看不出有任何理由,你为什么不能拥有的两个的ActiveRecord的和Mongoid机型在同一个应用程序。话虽这么说,我几乎可以肯定,你会碰到的问题,如果你尝试创建自己的ActiveRecord和Mongoid模型之间的关系。

I don't see any reason why you couldn't have both ActiveRecord and Mongoid models in the same application. That being said, I'm almost certain that you'll run into issues if you try to create relationships between your ActiveRecord and Mongoid models.

如果你的ActiveRecord模型有大量相互关联的,但更适合文档的结构,那么我会建议只是咬咬牙他们全部转换为Mongoid文件。我最近要做到这一点在(肥胖型)项目,这是显著比你想象那么紧张了。

If your ActiveRecord models are heavily inter-related, but better suited to a document structure, then I would suggest just biting the bullet and converting them all to Mongoid documents. I had to do this recently on a (large-ish) project, and it's significantly less stressful than you would think.

如果你有良好的单元测试你的模型,那么它应该是一个总的单元。如果你没有 - 首先编写单元测试,以确保它们通过与ActiveRecord的,然后开始迁移事情交给Mongoid

If you have good unit tests for your models, then it should be a total snap. If you don't - write your unit tests first, make sure they pass with ActiveRecord and then start migrating things over to Mongoid.

这篇关于我怎样才能在Rails 3中使用Mongoid和ActiveRecord的并行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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