【发布时间】:2011-09-26 07:09:18
【问题描述】:
我正在使用 rails 3,并使用 ActiveRecord 开始我的应用程序。现在,我有很多模型,并且关系开始变得复杂,有些可以用面向文档的结构更简单地表达,所以我想尝试迁移到 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
理想情况下,我希望先将我的 Item activerecord 模型替换为 Mongoid 文档,因此我的项目存储在 MongoDB 中,而我的 Users 和 Products 可以保留在我的 SQL DB 中
问题是,我不知道该怎么做。我这样做是否正确?
也许另一种选择是保留一个基础 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
谢谢!
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord mongodb mongoid