1
我有一個需要自動將ActiveRecord模型類轉換爲MongoDB文檔類。我能夠使用軌道生成器來讀取模型的屬性並生成新的document.rb。在內存中生成一個ruby類
如果ActiveRecord的模型類看起來象下面這樣:
class Project < ActiveRecord::Base
attr_accessible :completed, :end_date, :name, :start_date
end
然後,生成的類確認到Mongoid的結構將如下:
class ProjectDocument
field :name, type: String
field :start_date, type: Date
field :end_date, type: Date
field :completed, type: Boolean
field :created_at, type: Time
field :updated_at, type: Time
end
但我不想存儲不同的文檔文件,每個模型一個。我希望能夠在rails應用程序啓動時即時生成此文檔類。
這可能嗎?這種從內存中生成和使用類的方法建議嗎?我對AR模型結構的變化沒有限制;該文件是靈活的w.r.t數據結構和更改列將自動添加。
使用它的一個壞主意:結束作爲訪問者或字段名稱 – engineerDave
糟糕,採取了一點。這只是示例代碼;不在任何地方使用它。調整了這個問題。 –