Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Whether it's possible to model this in MongoMapper depends on whether there's data that needs to be stored in the <code>Like</code> model. If, as in your example, there isn't any data associated with the <code>Like</code> 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.</p> <p>You'd create your models like this:</p> <pre><code>class User include MongoMapper::Document key :name, String, :required =&gt; true key :liked_item_ids, Array many :liked_items, :in =&gt; :liked_item_ids, :class_name =&gt; "Item" end class Item include MongoMapper::Document key :name, String, :required =&gt; true many :fans, :class_name =&gt; "User", :foreign_key =&gt; :liked_item_ids end </code></pre> <p>Then you can do:</p> <pre><code>&gt;&gt; u = User.new( :name =&gt; 'emily' ) &gt;&gt; i = Item.new( :name =&gt; 'chocolate' ) &gt;&gt; u.liked_items &lt;&lt; i &gt;&gt; u.save &gt;&gt; u.liked_items =&gt; [#&lt;Item name: "chocolate", _id: 4b44cc6c271a466269000001&gt;] &gt;&gt; i.fans =&gt; [#&lt;User name: "emily", liked_item_ids: [4b44cc6c271a466269000001], _id: 4b44cc6c271a466269000002&gt;] </code></pre> <p>Unfortunately, what you can't do with this setup is add a like from the <code>Item</code> side of the relationship yet. However, there's an open issue on GitHub about creating a proper reverse for the <code>many :in</code> relationship which will be used, in this instance, as follows:</p> <pre><code>many :fans, :class_name =&gt; "User", :source =&gt; :liked_items </code></pre> <p>On the other hand, if there is information that needs to be stored in the <code>Like</code>, 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 <code>Like</code> embedded in the <code>User</code> model and a <code>has_many :through</code> relationship to create the link from <code>User</code> to <code>Item</code>. Unfortunately, support for this in MongoMapper is probably pretty far away.</p> <p>If you'd like to encourage support for behavior like this in MongoMapper, you can ask about it on the <a href="http://groups.google.com/group/mongomapper/" rel="nofollow noreferrer">mailing list</a> or open an issue on the <a href="http://github.com/jnunemaker/mongomapper/issues" rel="nofollow noreferrer">MongoMapper github repository</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload