Whether this can be modeled in MongoMapper depends on the availability of data that needs to be stored in the Like model. If, as in your example, there is no data associated with the Like model, there is a way. The latest update for MongoMapper added many-to-many support, although it is still in its early stages.
You would create your models as follows:
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 => [
Unfortunately, what you cannot do with this setting, add something else from the Item relationship side. However, GitHub has an open problem of creating the correct many :in inverse relationship, which will be used in this case as follows:
many :fans, :class_name => "User", :source => :liked_items
On the other hand, if there is information that should be stored in Like , for example, when a user liked an item, there is currently no way to simulate it. The ideal setup in this case (without considering what is supported in MongoMapper right now) will be similar to what you included in your question. You will need all three models with Like built into the User model, and the has_many :through connection to create a link from User to Item . Unfortunately, supporting this in MongoMapper is probably pretty far.
If you want to encourage support for such actions in MongoMapper, you can find out on the mailing list or open the MongoMapper github repository release .