2011-03-30 101 views
0

第一次使用DataMapper。我已經在MySQL數據庫中建立了一個表,並連接到這個表。我已經定義下面的映射:使用DataMapper獲取記錄

class Track_Scan 
    include DataMapper::Resource 

    property :item_id,     Integer 
    property :current_station_id,  Integer 
    property :next_station_id,   Integer 
end 

它返回的項目的右側 - 例如如果有五個記錄在數據庫中的一個給定的ID,Track_Scan.all(:item_id => my_id)將產生一組五個對象的 - 但是當我呼籲每一個在此,我看到了相同的對象五次:

#<Track_Scan:0x7fcbcfca59c0> 
#<Track_Scan:0x7fcbcfca59c0> 
#<Track_Scan:0x7fcbcfca59c0> 
#<Track_Scan:0x7fcbcfca59c0> 
#<Track_Scan:0x7fcbcfca59c0> 

,而不是五個不同的對象他們在current_station_idnext_station_id中有不同的值,正如它們在表中實際做的那樣。

任何幫助?

回答

1

您的模型缺少一個關鍵。如果你想使用組合鍵,你需要這樣做:

class Track_Scan 
    include DataMapper::Resource 

    property :item_id,   Integer, :key => true 
    property :current_station_id, Integer, :key => true 
    property :next_station_id, Integer, :key => true 
end 

此外,要求所有的車型後,你需要調用:

DataMapper.finalize 

希望這有助於