2012-05-09 108 views

回答

1

最後,我發現到現在最好的解決辦法是使用dm-is-reflective plugin: https://github.com/godfat/dm-is-reflective

它不會產生DataMapper的模式反映了現有的數據庫模式的代碼,但它的屬性訪問方法是自動可用(只要您繼續使用這個插件,當然)。

下面是使用的例子:你的回答

require 'data_mapper' 
require 'dm-is-reflective' 

DataMapper.setup(:default, "postgres://user:[email protected]/db") 

class Table 
    include DataMapper::Resource 

    is :reflective #activate dm-is-reflective 

    reflect #reflects eeach property. You can be more specific (look at plugin documentation) 
end 

DataMapper.finalize 

#Even if no field is defined, all of them are accessible 
entry = Table.first(nil, {:id => 469}) 
print entry.anotherField 
2

嘗試檢查https://github.com/yogo/dm-reflection或其任何叉..

+0

感謝@TomMeinIschmidt。我已經更新了答案,指出現在是官方存儲庫的Yogo fork。但我沒有嘗試過它的好運氣。 「dm-reflection」已經死了。在餘吳庫有一個應該用'DataMapper的工作分支1.0'但至少我沒有工作。它的安裝沒有工作,但我解決了,但仍然沒有工作。我認爲它有一些錯誤的依賴關係。我發現了另一個我現在發佈的解決方案。 –

相關問題