Possible to duplicate document in associations?

25 views
Skip to first unread message

Gary

unread,
May 20, 2012, 5:43:06 PM5/20/12
to mongo...@googlegroups.com
I have a edge-casey case where I have an association that is analogous to the following:

Course has_many :students
Student belongs_to :course

Both Course and Student have some number of keys.  Course has a name and a room number and a day of the week, for example.  

We're exposing a web service for people to get students and classes.  Clients will NEVER ask for student without asking for the course information to be included.  But, we can't use EmbeddedDocument and embed ourse within Student because Courses themselves are also first class objects.  We can, of course, use a standard association.  However clients may requests thousands of students at one time and doing the join for each Student isn't smart.

WHAT WE WANT is to save Course objectes into their own Mongo collection AND when we set Student.course, it copies the course into Student.course (including mongo _id)

Clients will never be updating this data, so any overhead generated to write speed can be completely ignored.

I'm going to hack up a gem for this soon unless someone can point out tot be that this has already been done somewhere.  Perhaps something like

Student belongs_to :course, :duplicates => true

Thanks!
Gary

Brian Hempel

unread,
May 21, 2012, 9:24:12 AM5/21/12
to mongo...@googlegroups.com
I don't think it's been gemified before.

Brian


--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en

Justin Dossey

unread,
May 21, 2012, 5:42:19 PM5/21/12
to mongo...@googlegroups.com
You might do well to have a join model, e.g.

Student has_many :enrollments
Enrollment has_one :course
Course has_many :enrollments

I would expect Enrollment to gather data about the student's relationship with the course (withdrawn, auditing, etc) as the codebase evolved.

Also, I would be wary of copying the whole Course object into the Student, as a Course change is practically inevitable.  If I were building it (without the Enrollment model) I would just store an Array of course IDs (ideally, human-readable) on each Student and cache to reduce Course lookups.

What you're doing will work, but maybe my $0.02 will help you avoid gray hair in the future.

Justin Dossey

Gary

unread,
May 23, 2012, 10:02:30 PM5/23/12
to mongo...@googlegroups.com
Looked through the code tonight.  Trying to decide between writing a new plugin like Plugins::DuplicatedDocument or overriding BelongsToAssociation.new to accept a :duplicate option and handle it there. 

Leaning toward Plugins::DuplicatedDocument as that sounds like it would be less intrusive and, perhaps, easier to re-use. Easier to describe too "DuplicatedDocuments are saved to the root_document AND their own collection"

Gary

unread,
May 23, 2012, 10:08:39 PM5/23/12
to mongo...@googlegroups.com
All sound advice indeed.  Student and Course and not our models, though, just an analogous relationship that people can grasp.  

Gary
Reply all
Reply to author
Forward
Message has been deleted
0 new messages