使用DataMapper获取记录
刚刚第一次使用DataMapper。我已经在 MySQL 数据库中设置了一个表并正在连接到该数据库。我定义了以下映射:
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_id
和 中具有不同值的五个不同对象next_station_id
正如它们在表中实际所做的那样。
有什么帮助吗?
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>
#<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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模型缺少一把钥匙。如果您想使用复合键,您需要执行以下操作:
此外,在需要所有模型之后,您需要致电:
希望这会有所帮助
Your model is missing a key. If you want to use a composite key you need to do this:
Also, after all your models are required you need to call:
Hope this helps