开发者

Getting records using DataMapper

开发者 https://www.devze.com 2023-02-21 01:36 出处:网络
Just using DataMapper for the first time. I have set up a table in a MySQL database and am connecting to that. I have defined the following mapping:

Just using DataMapper for the first time. I have set up a table in a MySQL database and am connecting to that. I have defined the following mapping:

class Track_Scan
    include DataMapper::Resource

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

It returns the right number of items - e.g. if there are five records in the DB for a given id, Track_Scan.all(:item_id => my_id) will yield a group of five objects - but when I call each on this, I see the same object five times:

#<Track_Scan:0x7fcbcfca59c0>
#<Track_Scan:0x7fcbcfca59c0>
#<Track_Scan:0x7fcbcfca59c0>
#<Track_Scan:0x7fcbcfca59c0>
开发者_StackOverflow#<Track_Scan:0x7fcbcfca59c0>

rather than five different objects with different values in their current_station_id and next_station_id as they actually do in the table.

Any help?


Your model is missing a key. If you want to use a composite key you need to do this:

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

Also, after all your models are required you need to call:

DataMapper.finalize

Hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消