Understanding MongoMappers many relationship
Asked Answered
G

1

11

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

Gadgeteer answered 10/4, 2011 at 16:41 Comment(2)
Owen--- you should move the answer to an answer and accept it yourselfSocioeconomic
Please do as @JesseWolgamott has said so this can disappear from my feed. Trying to help others and your question keeps popping up. Thanks!Anticlimax
G
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
Gadgeteer answered 27/10, 2011 at 8:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.