I am trying to figure out how to layout my database for a site I am working on. Here are my models:
class User
include MongoMapper::Document
// keys here
many :items
many :likes
end
class Item
include MongoMapper::Document
key :name, String, :required => true
many :likes
end
class Like
include MongoMapper::EmbeddedDocument
key :user_id, String, :required => true
end
I believe the Like should be embedded somewhere, but I am having a hard time picking one because of the functionality I would like to get out of it.
user = User.first
user.likes // should print out all the Items he liked
item = Item.first
item.likes // so I can count how many people like that item
Although the problem comes when using an EmbeddedDocument, you lose the find, and other helpful methods, and you can’t have it embedded in both models. So having it only in Item, I would need to run this (but can’t):
item = Item.first
item.likes.find_by_user_id(User.first._id)
undefined method find_by_user_id will be thrown. So if I was to embed this into my User, I still couldn’t do this.
likes = Like.all // will throw undefined `all`
So I came to the conclusing to maybe do it this way:
class Like
include MongoMapper::Document
key :user_id, String, :required => true
key :item_id, String, :required => true
belongs_to :user
belongs_to :item
end
But this seems like I am still trying to do things the old MySQL way. Could anybody give me a point on the most likely way to code this with MongoMapper?
Thanks in advance!
Whether it’s possible to model this in MongoMapper depends on whether there’s data that needs to be stored in the
Likemodel. If, as in your example, there isn’t any data associated with theLikemodel, there is a way. The most recent update to MongoMapper has added support for many-to-many relationships, although it’s still in the early stages.You’d create your models like this:
Then you can do:
Unfortunately, what you can’t do with this setup is add a like from the
Itemside of the relationship yet. However, there’s an open issue on GitHub about creating a proper reverse for themany :inrelationship which will be used, in this instance, as follows:On the other hand, if there is information that needs to be stored in the
Like, such as the date the user liked the item, there isn’t a way to model it currently. The ideal setup in this case (disregarding what’s supported in MongoMapper right now) would be similar to what you’ve included in your question. You’d need all three models, withLikeembedded in theUsermodel and ahas_many :throughrelationship to create the link fromUsertoItem. Unfortunately, support for this in MongoMapper is probably pretty far away.If you’d like to encourage support for behavior like this in MongoMapper, you can ask about it on the mailing list or open an issue on the MongoMapper github repository.