Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4237844
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:49:37+00:00 2026-05-21T02:49:37+00:00

I’ve been looking through this: http://datamapper.org/docs/find But haven’t been able to gleam what I’m

  • 0

I’ve been looking through this:

http://datamapper.org/docs/find

But haven’t been able to gleam what I’m looking for, though I know it’s quite simple.

I have two tables, scans and stations, with the relevant fields:

STATIONS - id (primary key), name
SCANS    - id (primary key), item_id, in_station, out_station

Where in_station and out_station are foreign keys to the id field in the stations table.

I have a Scan object

class Scan
    include DataMapper::Resource

    property :id,                       Integer, :key => true
    property :item_id,                  Integer
    property :in_station,               Integer
    property :out_station,              Integer
end

So right now, I can do Scan.all(:item_id => @barcode) to get all the scans on a particular item, and I’ve got the in_station id and out_station id. What’s the best way of getting the names, though, instead of ids. I assume it’s gotta be easier than for every scan calling Station.get(:id=> scan.in_station).

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?

EDIT:

I’ve almost got this working. I have a Station class:

class Station
    include DataMapper::Resource

    property :id, Integer, :key => true
    property :name, String
end

and I got rid of property :in_station and property :out_station in Scan and replaced with:

belongs_to :in_station,        :model => 'Station', :child_key => 'id'
belongs_to :out_station,       :model => 'Station', :child_key => 'id'

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:49:38+00:00Added an answer on May 21, 2026 at 2:49 am

    The assumption is that we don’t want to change the underlying SQL schema. So we have to tell DataMapper to use the existing foreign key names (in_station and out_station). The twist is that DataMapper will choke if the association name is the same as the child key. That’s why I have the ‘my_’ prefix on the association names.

    class Scan
      include DataMapper::Resource
    
      #rest of the properties
    
      belongs_to :my_in_station, :model => 'Station', :child_key => 'in_station'
      belongs_to :my_out_station, :model => 'Station', :child_key => 'out_station'
    end
    

    Usage

    s = Scan.get(id)
    s.my_in_station.name
    s.my_out_station.name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.