Note that there are some explanatory texts on larger screens.

plurals
  1. POReferencing foreign keys with DataMapper
    primarykey
    data
    text
    <p>I've been looking through this:</p> <p><a href="http://datamapper.org/docs/find" rel="nofollow">http://datamapper.org/docs/find</a></p> <p>But haven't been able to gleam what I'm looking for, though I know it's quite simple.</p> <p>I have two tables, scans and stations, with the relevant fields:</p> <pre><code>STATIONS - id (primary key), name SCANS - id (primary key), item_id, in_station, out_station </code></pre> <p>Where <code>in_station</code> and <code>out_station</code> are foreign keys to the <code>id</code> field in the <code>stations</code> table.</p> <p>I have a <code>Scan</code> object</p> <pre><code>class Scan include DataMapper::Resource property :id, Integer, :key =&gt; true property :item_id, Integer property :in_station, Integer property :out_station, Integer end </code></pre> <p>So right now, I can do <code>Scan.all(:item_id =&gt; @barcode)</code> to get all the scans on a particular item, and I've got the <code>in_station</code> id and <code>out_station</code> id. What's the best way of getting the <em>names</em>, though, instead of ids. I assume it's gotta be easier than for every scan calling <code>Station.get(:id=&gt; scan.in_station)</code>.</p> <p>This is easy enough using SQL, but how can I alter Scan/Station to either get the name or have a property that's a Station object, so I can do something like scan.station.name?</p> <p><em><strong>EDIT:</em></strong></p> <p>I've almost got this working. I have a Station class:</p> <pre><code>class Station include DataMapper::Resource property :id, Integer, :key =&gt; true property :name, String end </code></pre> <p>and I got rid of <code>property :in_station</code> and <code>property :out_station</code> in <code>Scan</code> and replaced with:</p> <pre><code>belongs_to :in_station, :model =&gt; 'Station', :child_key =&gt; 'id' belongs_to :out_station, :model =&gt; 'Station', :child_key =&gt; 'id' </code></pre> <p>Which I think/hope is saying "there's a field called in_station which is a foreign key into the Station table and one called out_station which is the same". Indeed, in_station and out_station are now instances of Station, BUT, they're the object. Even though in_station and out_station are different values, I'm getting the same object for each on every Scan. What am I doing wrong, how can I indicate that in_station and out_station are both references to Station but, when their ids are different, I expect different objects.</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. 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