真的想让我的头绕过以下情况。我通过屏幕抓取足球结果并将其保存到模型(结果),目前为止确定....我在模型之间建立了一些关联,我想从相关模型中获取所有ID并保存到我的结果中模型..我的模型设置为使从另一个模型获取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)
需要拉出值的话..
,但我不....... – Richlewis 2013-05-14 11:21:13
我的意思是,你可以使用一个;你的答案) – yoones 2013-05-14 11:43:25