2013-10-06 34 views
2

我有一點"just for fun" Rails app,我正在從Active Record和SQLite遷移到Ruby Object Mapper。對於我來說,探索數據映射器模式影響我的代碼的方式大多是一個機會。與Ruby對象映射器的聯繫

我有課程模型,遊戲模型和Score模型。在計算課程記錄時,我需要獲得課程對象及其所有相關遊戲和每個遊戲的所有分數。

我找到了an example of creating a joined relation,但後來我似乎找不到任何如何爲該關係編寫映射器的示例,因此我無法真正將這些數據返回。

我ROM的模式是這樣的:

base_relation :courses do 
    repository :main 
    attribute :id,   Integer 
    attribute :name,  String 
    attribute :created_at, Time 
    attribute :updated_at, Time 
    key :id 
end 

base_relation :games do 
    repository :main 
    attribute :id,   Integer 
    attribute :course_id, Integer 
    attribute :played_at, Time 
    attribute :created_at, Time 
    attribute :updated_at, Time 
    key :id 
    key :course_id 
end 

我想做一個查詢在那裏我能得到一個給定的課程及其所有相關的遊戲。就像:

env[:courses].restrict(id: 1).join(env[:games]).one 

但我一直沒能找到指定連接的正確語法,我只知道公理支持連接在內存中。

有誰知道一個很好的例子,用Ruby Object Mapper來讀寫連接數據嗎?

+0

所以你現在正在使用Ruby Object Mapper和Redis Store?我對你有什麼以及你想達到什麼感到困惑。發佈一些示例代碼來掌握你正在嘗試做什麼也是有幫助的。 – phoet

+0

@phoet對不起,因爲這個問題的目的,redis是無關緊要的。我實際上在生產中使用[axiom-redis-adapter](https://github.com/hqmq/axiom-redis-adapter?source=c)和一個簡單的[yaml適配器](https://github.com/ solnic/rom-demo/blob/master/lib/yaml_adapter.rb)在我的測試中。我編輯了上面的問題以包含一些示例代碼。 – mmmries

回答

3

目前ROM不支持映射連接關係OOTB。 ROM中會有一個名爲nest/unnest的公理出現了一個新特性,用來以合理的方式映射連接的關係。

現在,它需要很多hackery,這就是爲什麼我們決定推遲該功能,並等待公理的巢/ unnest。

+0

我應該在nest/unnest上添加這項工作:https://github.com/dkubb/axiom/pull/40 – dkubb

+0

...並且完成了:) – solnic