When to use Embedded Documents?
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::Embe开发者_如何学JAVAddedDocument
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 Like
model. If, as in your example, there isn't any data associated with the Like
model, 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:
class User
include MongoMapper::Document
key :name, String, :required => true
key :liked_item_ids, Array
many :liked_items, :in => :liked_item_ids, :class_name => "Item"
end
class Item
include MongoMapper::Document
key :name, String, :required => true
many :fans, :class_name => "User", :foreign_key => :liked_item_ids
end
Then you can do:
>> u = User.new( :name => 'emily' )
>> i = Item.new( :name => 'chocolate' )
>> u.liked_items << i
>> u.save
>> u.liked_items
=> [#<Item name: "chocolate", _id: 4b44cc6c271a466269000001>]
>> i.fans
=> [#<User name: "emily", liked_item_ids: [4b44cc6c271a466269000001], _id: 4b44cc6c271a466269000002>]
Unfortunately, what you can't do with this setup is add a like from the Item
side of the relationship yet. However, there's an open issue on GitHub about creating a proper reverse for the many :in
relationship which will be used, in this instance, as follows:
many :fans, :class_name => "User", :source => :liked_items
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, with Like
embedded in the User
model and a has_many :through
relationship to create the link from User
to Item
. 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.
you might want to read the document "Embed vs. Reference" on mongodb.org: http://www.mongodb.org/display/DOCS/Schema+Design#SchemaDesign-Embedvs.Reference
精彩评论