2011-03-30 102 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 

希望这有助于