Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your model seems a bit confused:</p> <blockquote> <p>Let's say I have an Orchard full of Apple Trees. Each Tree has many Apples. Each Apple has many Seeds. Therefore each tree has many Seeds through its Apples.</p> </blockquote> <p>So a tree has many apples, an apple belongs to a tree and has many seeds, and a seed belongs to an apple (and ultimately a single tree).</p> <p>We can almost (but not quite) take that language as it is and use it to create the associations. After a little translation to get the syntax right we get this:</p> <pre><code># Trees in the orchard. class Tree include DataMapper::Resource property :id, Serial has n, :apples # "a tree has many apples" has n, :seeds, :through =&gt; :apples end # Apples on a Tree. class Apple include DataMapper::Resource property :id, Serial belongs_to :tree # "an apple belongs to a tree..." has n, :seeds # "...and has many seeds" end # Seeds in an Apple class Seed include DataMapper::Resource property :id, Serial belongs_to :apple # "and a seed belongs to an apple" end </code></pre> <p>In your code you have seeds having multiple apples and trees, which doesn't really make any sense.</p> <p>As for querying:</p> <blockquote> <p>But how many Apples does Tree #2 have?</p> </blockquote> <p>Assuming you mean the <code>Tree</code> with <code>id == 2</code>:</p> <pre><code>tree_2 = Tree.get(2) apples_of_tree_2 = tree_2.apples # this gives an array of apples count_of_apples_of_tree_2 = tree_2.apples.count </code></pre> <blockquote> <p>How many Seeds does Tree #4 have?</p> </blockquote> <p>The association we added to the <code>Tree</code> model <code>has n, :seeds, :through =&gt; :apples</code> means we have a <code>seeds</code> method available in <code>Tree</code> objects.</p> <pre><code>Tree.get(4).seeds.count </code></pre> <blockquote> <p>How many Apples in total? How many Seeds in total?</p> </blockquote> <p>Simply:</p> <pre><code>Apple.count # note singular not plural (it's a class method on Apple) Seed.count </code></pre> <p>Try loading this new model into irb (you might need to delete your <code>orchard.db</code> file when you change the model), and then playing around with some of the <a href="http://datamapper.org/docs/find.html" rel="noreferrer">queries</a> and <a href="http://datamapper.org/docs/create_and_destroy.html" rel="noreferrer">creation methods</a>, hopefully that'll give you a better idea of what's going on.</p> <h2>Creating associations</h2> <p>(See the section "Adding To Associations" on <a href="http://datamapper.org/docs/associations.html" rel="noreferrer">the Associations page</a>.)</p> <p>To add an existing <code>Apple</code> to a <code>Tree</code>:</p> <pre><code>a_tree.apples &lt;&lt; an_apple </code></pre> <p>Note that a <code>Tree</code> isn't associated with a single <code>Apple</code> but a collection (it <code>has n</code> <code>Apples</code>), so the method created is <code>apples</code> (i.e. it's pluralized), and there's no method <code>apple</code> which is why you're seeing the no method error.</p> <p>You can also create a new <code>Apple</code> associated with a <code>Tree</code> directly:</p> <pre><code>a_tree.apples.new #created but not saved to database a_tree.apples.create #created and saved to database </code></pre> <p>You can also create the association the other way round, from the <code>Apple</code> side:</p> <pre><code>an_other_apple = Apple.new an_other_apple.tree = a_tree </code></pre> <p>but you need to be careful doing it this way, as the new apple won't show up in the <code>a_tree</code>s collection of <code>Apples</code> (<code>a_tree.apples</code> won't include <code>an_other_apple</code>). In order for it to appear you need to save the apple, and then call <code>reload</code> on the <code>Tree</code>:</p> <pre><code>an_other_apple.save a_tree.reload </code></pre> <p>You need to watch out with this, as you can end up with an <code>Apple</code> that appears to be in two <code>Tree</code>s at the same time if you're not careful.</p>
 

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