Note that there are some explanatory texts on larger screens.

plurals
  1. POFetching values via foreign keys with DataMapper
    text
    copied!<p>I have two tables, <code>nodes</code> and <code>terms</code>.</p> <p>The relevant fields in <code>nodes</code> are: <code>nid</code> (primary key) and <code>value</code><br /> In <code>terms</code>, they are: <code>value</code>, <code>tid</code>, and <code>nid</code>, where <code>value</code> and <code>tid</code> together are the primary key and <code>nid</code> is a foreign key referencing <code>nodes.nid</code>.</p> <p>I want to add records to <code>terms</code>. I have the <code>tid</code> and <code>nid</code>, and the <code>value</code> I want to pull from the corresponding <code>node</code> - e.g. look up the <code>value</code> for a given <code>nid</code> in <code>node</code> and then put that as the <code>value</code> in <code>terms</code>.</p> <p>A way to do this in SQL might be:</p> <pre><code>INSERT INTO terms(tid, nid, value) values(mytid, mynid, ( select value from nodes where nid=mynid )); </code></pre> <p>Could somebody help me do this with DataMapper?</p> <pre><code>class Node include DataMapper::Resource property :nid, Serial, :key =&gt; true property :value, Integer end class Term include DataMapper::Resource property :tid, Integer, :key =&gt; true # how do I define nid and value? end # and then what do I give to Term.new or Term.create and how? </code></pre> <p>If anyone could point me to a good tutorial of DataMapper as well, I'd appreciate it. I've been using their online docs, but I've found the situations I find myself in are rarely covered there.</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