3

I have a need for a certain model to contain a reference to a document. Most of the model could be stored in postgres. The model is for a "level" in a game. I'd like to store the level data itself inside of a document, which makes more sense than making a complex tree in sql.

I am able to use postgres with mongoid installed; however, after installing the mongoid gem I seem to only be able to scaffold mongoid (non active record) documents.

The problem is that I have references to other tables, and I don't neccesarily know how to link that up within a mongoid model.

Questions:

  1. How can I force scaffolding to occur with active record instead of mongoid or vice versa. Edit: partly answered here: Using Active Record generators after Mongoid installation? (2nd answer works, but I don't know how to go back and forth easily)

  2. Is there an easy way to reference a document from an active record model (I know the documentation said don't mix them, but it is ideal for what I am trying to do).

  3. If it is not possible to mix them, then how should I make a document be referenced from a postgres/active record table. In other words how can I get both pieces of data at the same time?

Thanks!

Community
  • 1
  • 1
Parris
  • 17,833
  • 17
  • 90
  • 133
  • Why not just store the level data as a JSON blob in either the same table or an adjoining table in the same postgresql database? – yfeldblum May 29 '12 at 15:46
  • That would be doable, but I just feel that storing a json blob inside of postgres isn't the right tool for the job. It is also unreadable once it goes in there. Then when you pull it out, you need to parse it everytime. Then stringify it once you put it back in. I mean if you are creating a document, and not using a document oriented db, then what is the point of document oriented db's? Speed is great, but it is not like postgres is slow. – Parris May 29 '12 at 19:21

1 Answers1

7

Regarding your first question, the ideal solution would be something along the lines of the first answer in the referenced post. However, instead of a generating a migration, generate a model instead. So when you want an Active Record model simply run:

rails g active_record:model

As for your second and third questions, to associate an Active Record model with a Mongoid document simply store the ObjectId as a string in the model. Then, when you get retrieve a record make a new ObjectId out of the string and use that to query for the related document.

You can create object ids out of the strings like this:

BSON::ObjectId.from_string("object_id_string")

There isn't really an easy way to easily follow intra-orm relations when mixing and matching between ActiveRecord and Mongoid though so I'm afraid that will have to be done via Ruby code.

The models you define in rails either extend one ORM's base class or the other and they don't know about one another. There may be projects out there that act as a layer on top of these ORMs but I am not familiar with any that exist at the moment.

Tyler Brock
  • 29,626
  • 15
  • 79
  • 79