0
我正在使用Ruby(而不是Rails)和PostgeSQL,并且一直在敲打我的头几个小时,试图弄清楚如何在知道列和列时指定字段的值你试图交叉引用的行。查询具有已知列和行的特定单元格
我曾尝试以下:
一个包含城市和连接距离,类似于数据库:
Cities city1 city2 city3 city4
city1 0 17 13 6
city2 17 0 7 15
city3 . . .
city4 . . .
,我曾尝试用下面的代码打转转:
array.each { |city| #array being the array containing the sorted cities
from_city = city
query { |row|
#row is a hash containing each city as key and distance as val
#first row: {"Cities"=>"city1", "city1"=>"0", "city2"=>"17",...
#I have tried doing a row.each and grabbing the value of the specified city key,
but that doesn't work..
}
}
是有更好的方法去做这件事?基本上所有我需要知道的是,当我知道我想要使用的两个城市时,如何获得距离值,并将其分配给变量distance
。
你用什么来连接数据库?我强烈建议寻找[Sequel](http://sequel.jeremyevans.net/)宝石。它非常强大和灵活。而且,为了帮助你的术语,数据库有行和*字段*,而不是单元格。 – 2014-11-25 07:01:24
我正在使用pg gem。目前为止我的工作很好,但我只是坚持这一点。仍然是Ruby和数据库的新手..:/ – Luminusss 2014-11-25 07:06:11
假设你知道'to_city',解决方案就像'row [to_city]'一样简单。 – mudasobwa 2014-11-25 08:16:51