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!