Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding MongoMappers many relationship

Ok, so firstly Im not very in the know when it comes to Ruby, Rails, Mongo or even ActiveRecord so I apologise if these questions are fairly basic. Firstly, I have an "Event" model and I have added a many relationship to my "Comment" model. Now it is my (basic) understanding of mongo (or any document db) that foreign key lookups (or whatever they are called in mongo) are not advised, instead opting for storing as much as possible in a single document. With this in mind I would expect to have a single Events collection with the Comment JSON/BSON embedded in each document i.e.

event:

{
 Title: "some event"
 Comments : [
    { user_id: [object id], comment: "comment"}, 
    { user_id: [object id], comment: "other comment"}
 ]
}

Instead I am seeing that a comments collection has been created as well as the event collection. Would this be the correct way to map this relationship, if so how do I stop the comment collection been created and instead have the info embedded directly into the event? My guess is that I have to tell my Comment mapping not to have a _id property and thus not belong to its own collection.


EDIT: I found out that what I was looking for here in my first question was to use MongoMapper::EmbeddedDocument instead of including MongoMapper::Document


Secondly, I want users to be able to flag themselves as "attending" or just "interested" an event, which I assumed would be modelled as an array of reference user_id's on the event. So I initially thought it would be mapped as two "many" relationships, but as far as I can tell the name of the constant I am passing to the many method is used to create the getter/setters, which wouldn't work for me if I had two many relationships for the same type.

In other words, if I have this mapping:

class Event
   many :users
end

Then it's my understanding that I will then have getters and setters generated for the users property. So I can do something like:

event.users << someAttendingUser

This is all good, and at this point in time I would want the reference to the user to be stored and not the whole user BSON (unlike with the comments example above). Now the problem is how do I do this when I have two many mappings to the user collection, as with my need for both "attending" and "interested" users?

So to summarise this rambling:

  • Sometimes I want to store many relationships directly as BSON in the orinal document, and not as a reference to a document in another collection. How do I do this?

  • Sometimes I want to store many relationships of the same type on a document and I DO want them to be references to a document in another collection. How do I do this?

Hope this made sense, and I apologise if Im asking the obvious here.


EDIT 2:

Ok, well I really did search before asking this question, but it appears that I have now found the answer to both of my problems so I will close the question myself.

The second part required me to specify a class name option for the mapping i.e.

class Event
   many :attendees, :class_name  => "User"
   many :interested, :class_name  => "User"
end

like image 825
Owen Avatar asked Apr 10 '11 16:04

Owen


1 Answers

Ok, well I really did search before asking this question, but it appears that I have now found the answer to both of my problems so I will close the question myself.

The second part required me to specify a class name option for the mapping i.e.

class Event
   many :attendees, :class_name  => "User"
   many :interested, :class_name  => "User"
end
like image 117
Owen Avatar answered Sep 30 '22 19:09

Owen