Skip to content

Instantly share code, notes, and snippets.

View dmitryame's full-sized avatar

Dmitry Amelchenko dmitryame

View GitHub Profile
@dmitryame
dmitryame / followers association with mongoid twitter style
Created April 2, 2011 20:29
followers association with mongoid twitter style
What is the best way to model user/follower (twitter style) relationships with mongo? Most prominent solution is to embed an array of followers ids in every user object. I'm an idealist, and believe that if we are modeling twitter, we eventually will run into a situation when a user has millions of followers which will blow. The relational many_to_many is not easy to model with mongo, mongo_id tries to offer some implementation of basic associations, but the many_no_many, or has_many through is not there yet and I doubt it ever will be.
My solution is a compromise between relational normalization and "embed everything in one document".
class User
include Mongoid::Document
include Mongoid::Timestamps
# user will have many followerships, each will embedd a follower user,
# the user_id is stored on the followership object