Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top