真的想弄清楚以下情况。我正在通过屏幕抓取抓取足球结果并将它们保存到模型(结果),到目前为止还可以......我在模型之间设置了一些关联,我想从关联模型中获取所有 id 并保存到我的结果中模型..我的模型是这样设置的
class Fixture < ActiveRecord::Base
attr_accessible :away_team, :fixture_date, :home_team, :kickoff_time, :prediction_id
end
class Prediction < ActiveRecord::Base
attr_accessible :away_score, :away_team, :fixture_id, :home_score, :home_team, :score
has_one :fixture
has_one :result
end
class Result < ActiveRecord::Base
attr_accessible :away_score, :away_team, :fixture_date, :home_score, :home_team, :prediction_id
end
我的屏幕刮痕看起来像这样
def get_results
doc = Nokogiri::HTML(open(RESULTS_URL))
days = doc.css('.table-header').each do |h2_tag|
date = Date.parse(h2_tag.text.strip).to_date
matches = h2_tag.xpath('following-sibling::*[1]').css('tr.report')
matches.each do |match|
home_team = match.css('.team-home').text.strip
away_team = match.css('.team-away').text.strip
score = match.css('.score').text.strip
home_score, away_score = score.split("-").map(&:to_i)
Result.create!(home_team: home_team, away_team: away_team, score: score, fixture_date: date, home_score: home_score, away_score: away_score)
end
end
end
因此,在创建结果之前,我需要从与正确结果(足球比赛)相对应的夹具模型中获取预测 ID,然后在保存所有其他属性时同时保存它们。我希望这是有道理的..
谢谢
编辑
好吧,我已经到了这一步
fixture = Fixture.where(fixture_date: date, home_team: home_team, away_team: away_team).first
prediction_array = Prediction.where(fixture_id: fixture.id)
然后需要提取值..