2011-03-31 38 views
0

外鍵我一直在尋找通過這個:引用與DataMapper的

http://datamapper.org/docs/find

但一直沒能閃耀正是我要找的,雖然我知道這是很簡單的。

我有兩個表,掃描和車站,與相關領域:

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

in_stationout_station外鍵的id字段中stations表。

我有一個Scan對象

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_id => @barcode)工作再上一個特定項目所有的掃描,我已經得到了in_station ID和out_station ID。不過,取代名稱的最佳方式是什麼?我認爲這比每次掃描Station.get(:id=> scan.in_station)都要容易得多。

這是很容易使用SQL足夠了,但我怎麼可以改變掃描/站要麼得到的名字,或是有這是一個站對象的屬性,所以我可以做類似scan.station.name?

編輯:

我幾乎得到了這個工作。我有一個站類:

class Station 
    include DataMapper::Resource 

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

和我擺脫property :in_stationScanproperty :out_station,取而代之的是:

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

這一點我想/希望的說:「有一個字段名爲in_station這是一個將外鍵插入到電臺表格中,並將其中一個叫做out_station,它們是相同的「。事實上,in_station和out_station現在是Station的實例,但是,它們是對象。儘管in_station和out_station是不同的值,但我在每個掃描中都會得到相同的對象。我做錯了什麼,我怎麼能指出in_station和out_station都是對站的引用,但是當他們的id不同時,我期望不同的對象。

+0

看看在數據庫中搜索表創建的領域。就像我下面所說的,儘管名稱'child_key'暗示了另外一種情況,您需要使用像下面所示的名稱。 你的'belongs_to'行說的是「掃描屬於一個名爲'in_station'的關聯站,並且將加載一個名爲'id'的掃描屬性的'id'的站。這不是你想要的。你想要「掃描屬於一個名爲'in_station'的關聯站,並將加載一個名爲'in_station_id'的Scan屬性的'id'。 – Larsenal 2011-03-31 20:00:43

回答

1

的假設是,我們不希望改變底層的SQL模式。所以我們必須告訴DataMapper使用現有的外鍵名(in_station和out_station)。扭曲的是,如果關聯名稱與子鍵相同,則DataMapper將會窒息。這就是爲什麼我在關聯名稱上有'my_'前綴。

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 

使用

s = Scan.get(id) 
s.my_in_station.name 
s.my_out_station.name 
+0

好的,請原諒我的無知,但是一旦我完成了這一步(我將child_keys改爲'id',因爲這是站內的字段名稱,或者應該顛倒過來),我該如何訪問該名稱? 我有一堆掃描對象,我想要他們被掃描的電臺名稱和他們被掃描出來的名字。 – hsiu 2011-03-31 19:07:49

+0

不要更改:child_key。 DataMapper已經知道'Station'模型的關鍵是'id'。我將添加一個使用示例。 – Larsenal 2011-03-31 19:35:32

+0

如果我更改:child_key的任何內容_but_id我得到一個錯誤 - 堆棧級別太深或字段列表中的未知列'in_station_id'。 – hsiu 2011-03-31 20:01:23

2

如何這樣做:

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 

應該在符合您的架構爲你工作。

+0

我剛剛更新了問題,缺乏清晰度。 in_station和out_station和id,並參考站表/模型中的id。 我希望能夠執行'Scan.all(:item_id => @barcode)'來獲取特定項目的所有掃描結果,然後通過這些掃描並獲取它們在其中掃描的位置的名稱並出去。現在我可以得到它在哪裏掃描進出的ID,我只是喜歡這個名字,而id是一個有名字的電臺表的外鍵。我如何告訴DataMapper in_station和out_station是站點? – hsiu 2011-03-31 18:21:48