views:

878

answers:

3

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!

A: 

dear garrett

you should use a little Reflection to read/write data into embedded resource. all you should do is calling method from inside of the assembly. for example

System.Reflection.Assembly asm = Assembly.GetExecutingAssembly();
System.IO.Stream xmlStream = asm.GetManifestResourceStream"Hajloo.FOSS.UnicodeConverter.EnterpriseAppUnit.ucd.all.flat.xml");
XmlReader reader = XmlReader.Create(xmlStream, settings);

my model read from a xml document ut you should call your Like method

I've writen a complete description for reading from embeddd resource at http://hajloo.wordpress.com/2009/12/14/%e2%80%aahow-to-read-embedded-resource-in-c/

Nasser Hadjloo
This question is about modeling a domain in MongoMapper, a specific Ruby NoSQL library, not about C# EmbeddedResources.
Emily
+4  A: 

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.

Emily
This does seem to be the makeshift solution for now, I am going to let this run a few more days to see if I get any more responses before I pick this as the chosen answer! Thanks!
Garrett
After a little more investigation, I discovered the John opened an issue on GitHub for a more suitable reverse of the `many :in` relationship, so that's coming soon as well. Still doesn't help if you need to save information in the `Like` though.
Emily
MongoMapper issues are no longer tracked at Github, instead they're using Pivotal Tracker, here: http://www.pivotaltracker.com/projects/33576 Actually, PivotalTracker is an Agile Project Management application, not intended for issue tracking, so I think this is a poor choice on the part of the MongoMapper developers.
Patrick Klingemann
+1  A: 

you might want to read the document "Embed vs. Reference" on mongodb.org: http://www.mongodb.org/display/DOCS/Schema+Design#SchemaDesign-Embedvs.Reference

railsmongo