我使用的是rails 3,我的应用程序是从ActiveRecord开始的.现在,我有很多模型,它们之间的关系开始变得复杂,有些可以用面向文档的 struct 更简单地表达,所以我想try 迁移到MongoDB并使用Mongoid.
我一直听说,您不必全部使用MongoDB或什么都不使用,但可以在迁移时并行使用这两个.不过,我不知道如何从文件中了解这一点.
例如,我有:
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文档替换我的Item activerecord模型,这样我的项目就存储在MongoDB中,我的Users
和Products
就可以保存在我的SQL数据库中
问题是,我不知道该怎么做.我这样做对吗?
也许另一种 Select 是保留基本AR项目
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
谢谢