我正在使用rails 3,並開始與ActiveRecord我的應用程序。現在,我有很多模型,關係開始變得複雜,有些可以更簡單地用面向文檔的結構來表示,所以我想嘗試遷移到MongoDB並使用Mongoid。如何在Rails 3中並行使用Mongoid和ActiveRecord?
我一直聽說你不需要使用所有的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文件替換我的項目ActiveRecord的模型開始,所以我的項目都存儲在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
謝謝!
我喜歡你的天真的做法!你在生產中有類似的東西嗎?時間過去了嗎? –
任何性能基準測量? –