Ruby on rails 使用DataMapper引用外键

Ruby on rails 使用DataMapper引用外键,ruby-on-rails,ruby,datamapper,Ruby On Rails,Ruby,Datamapper,我一直在看这个: 虽然我知道我要找的东西很简单,但我还是没能找到 我有两个表,扫描和工作站,以及相关字段: STATIONS - id (primary key), name SCANS - id (primary key), item_id, in_station, out_station 其中in_station和out_station是stations表中id字段的外键 我有一个扫描对象 class Scan include DataMapper::Resource

我一直在看这个:

虽然我知道我要找的东西很简单,但我还是没能找到

我有两个表,扫描和工作站,以及相关字段:

STATIONS - id (primary key), name
SCANS    - id (primary key), item_id, in_station, out_station
其中
in_station
out_station
stations
表中
id
字段的外键

我有一个
扫描
对象

class Scan
    include DataMapper::Resource

    property :id,                       Integer, :key => true
    property :item_id,                  Integer
    property :in_station,               Integer
    property :out_station,              Integer
end
所以现在,我可以做
Scan.all(:item\u id=>@barcode)
来获得对某个特定项目的所有扫描,我得到了
in\u station
id和
out\u station
id。但是,获得名称而不是id的最佳方法是什么。我想这比每次扫描调用
Station.get(:id=>scan.in_Station)
都要容易

使用SQL很容易做到这一点,但是如何更改Scan/Station以获取名称或具有Station对象属性,以便执行类似Scan.Station.name的操作

编辑:

我几乎把它弄好了。我有一节车站课:

class Station
    include DataMapper::Resource

    property :id, Integer, :key => true
    property :name, String
end
我去掉了
property:in_station
property:out_station
in
Scan
,取而代之的是:

belongs_to :in_station,        :model => 'Station', :child_key => 'id'
belongs_to :out_station,       :model => 'Station', :child_key => 'id'
我认为/希望是这样的:“有一个字段叫做in_station,它是station表中的外键,还有一个字段叫做out_station,它是相同的”。事实上,in_station和out_station现在是station的实例,但它们是对象。尽管in_station和out_station是不同的值,但每次扫描都会得到相同的对象。我做错了什么,我怎么能指出in_station和out_station都是对station的引用,但是,当它们的ID不同时,我期望不同的对象。

这样做怎么样:

class Station
  include DataMapper::Resource

  property :id, Serial
  # rest of the properties

  has n, :scans
end

class Scan
  include DataMapper::Resource

  property :id, Serial
  # rest of the properties

  belongs_to :station
end
然后您只需执行此操作即可访问关联的站点:

station = Station.create
scan    = station.scans.create

scan.station # returns the associated station
这应该适合您的模式。

这样做怎么样:

class Station
  include DataMapper::Resource

  property :id, Serial
  # rest of the properties

  has n, :scans
end

class Scan
  include DataMapper::Resource

  property :id, Serial
  # rest of the properties

  belongs_to :station
end
然后您只需执行此操作即可访问关联的站点:

station = Station.create
scan    = station.scans.create

scan.station # returns the associated station

这在匹配您的模式时应该对您有用。

假设我们不想更改基础SQL模式。因此,我们必须告诉DataMapper使用现有的外键名称(in_station和out_station)。问题是,如果关联名称与子键相同,DataMapper将阻塞。这就是为什么我在协会名称上有“我的”前缀

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
用法


假设我们不想更改底层SQL模式。因此,我们必须告诉DataMapper使用现有的外键名称(in_station和out_station)。问题是,如果关联名称与子键相同,DataMapper将阻塞。这就是为什么我在协会名称上有“我的”前缀

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
用法


我刚刚更新了这个问题,不够清晰。输入站和输出站以及id,并参考站表/模型中的id。我希望能够执行
Scan.all(:item_id=>@barcode)
以获取特定项目的所有扫描,然后通过这些扫描并获取扫描进出的位置的名称。现在我可以得到它被扫描进和扫描出的位置的id,我只想要这个名字,id是stations表的外键,它有一个名字。我怎样才能告诉DataMapper in_station和out_station都是站点?我刚刚更新了这个问题,不够清晰。输入站和输出站以及id,并参考站表/模型中的id。我希望能够执行
Scan.all(:item_id=>@barcode)
以获取特定项目的所有扫描,然后通过这些扫描并获取扫描进出的位置的名称。现在我可以得到它被扫描进和扫描出的位置的id,我只想要这个名字,id是stations表的外键,它有一个名字。我如何告诉DataMapper in_station和out_station都是站点?好吧,请原谅我的无知,但一旦我这样做了(我将:child_键更改为“id”,因为这是站点中的字段名,或者应该颠倒),我如何访问该名称?我有一堆扫描对象,我想要他们被扫描的站点的名称和他们被扫描出来的站点的名称。不要更改:child_键。DataMapper已经知道“Station”模型的键是“id”。我将添加一个用法示例。如果我将:child\u键更改为除id以外的任何值,我将得到一个错误-堆栈级别太深或字段列表中的未知列“in\u station\u id”。请将
:child\u键
视为外键。因此,您可以使用它来表示“将具有此名称的外键添加到当前模型”。这些迁移是什么?给定的模型与mysql后端匹配。表中的任何位置都没有名为“in_station_id”的键,扫描表中只有in_station和out_station,两者都引用stations表中简称为“id”的字段。如果我将子项设置为“in_station”和“out_station”,我会得到一个错误:堆栈级别太深*文件:many_to_one.rb*位置:target_key*行:271如果我将它们都设置为id,我会得到一个station对象,但对于in_station和out_station都是相同的,即使DB中指示了不同的station id。好的,请原谅我的无知,但一旦我这样做了(我将:child_键更改为'id',因为这是Station中的字段名,或者应该反转),我如何访问该名称?我有一堆扫描对象,我想要他们被扫描的站点的名称和他们被扫描出来的站点的名称。不要更改:child_键。DataMapper已经知道“Station”模型的键是“id”。我将添加一个用法示例。如果我将:child\u键更改为除id以外的任何值,我将得到一个错误-堆栈级别太深或字段列表中的未知列“in\u station\u id”。请将
:child\u键
视为外键。因此,您可以使用它来表示“将具有此名称的外键添加到当前模型”。这些迁移是什么?给定的模型与mysql后端匹配。没有钥匙