2

I have a little "just for fun" Rails app that I am migrating from Active Record and SQLite to Ruby Object Mapper. This is mostly a chance for me to explore the way that the data mapper pattern affects my code.

I have a Course model, a Game model and a Score model. When calculating course records I need to get a course object along with all of its associated games and all of the scores for each of those games.

I found an example of creating a joined relation but then I couldn't seem to find any examples of how to write a mapper for that relation so I'm not able to actually get that data back out.

My ROM schema looks like this:

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

And I want to do a query where I can get a given course with all of its related games. Something like:

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

But I haven't been able to find the right syntax for specifying a join, I just know that axiom supports joins in memory.

Does anyone know of a good example of doing reads and write with joined data from Ruby Object Mapper?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mmmries
  • 967
  • 8
  • 12
  • so right now you are using Ruby Object Mapper with a Redis Store? I am a little confused about what you have and what you want to achieve. it would also be helpful to post some example code to get a grasp of what you are trying to do. – phoet Oct 06 '13 at 13:38
  • @phoet Sorry for muddying the waters, redis is irrelevant for the purposes of this question. I am actually using [axiom-redis-adapter](https://github.com/hqmq/axiom-redis-adapter?source=c) in production and a simple [yaml adapter](https://github.com/solnic/rom-demo/blob/master/lib/yaml_adapter.rb) in my tests. I've edited the question above to include some sample code. – mmmries Oct 08 '13 at 01:38

1 Answers1

3

Currently ROM doesn't support mapping joined relations OOTB. There's a new feature coming up in axiom called nest/unnest that ROM will use to map joined relations in a sane way.

Right now it would require a lot of hackery that's why we decided to postpone that feature and wait for axiom's nest/unnest.

solnic
  • 5,733
  • 2
  • 23
  • 19
  • I should add that work on nest/unnest is proceeding in this pull request: https://github.com/dkubb/axiom/pull/40 – dkubb Oct 08 '13 at 16:09