慢慢实现我想要实现的目标。我正在通过屏幕抓取抓取数据并希望将数据保存到我的模型中,我有两列,home_team 和 away_team。到目前为止,我抓取了数据。
FIXTURE_URL = "http://www.bbc.co.uk/sport/football/premier-league/fixtures"
def get_fixtures # Get me all Home and away Teams
doc = Nokogiri::HTML(open(FIXTURE_URL))
home_team = doc.css(".team-home.teams").map {|h| h.text.strip }
away_team = doc.css(".team-away.teams").map {|a| a.text.strip }
#team_clean = Hash[:home_team => home_team, :away_team => away_team]
#team_clean = Hash[:team_clean => [Hash[:home_team => home_team, :away_team => away_team]]]
end
我已经讨论了两种将数据放入散列的方法,一种是散列,另一种是散列中的散列,我不确定我需要哪一个(如果有的话?)
因此,如果我想保存从我的 home_team 收到的数据,我会运行一个 rake 任务来执行此操作
def update_fixtures #rake task method
Fixture.destroy_all
get_fixtures.each {|home| Fixture.create(:home_team => home )}
end
我想要实现的是能够同时拯救 home_team 和 away_team。我是否需要访问散列中的数据,如果需要,如何访问?这里有点丢失,但这是我第一次尝试这个
任何帮助表示赞赏