真的想讓我的頭繞過以下情況。我通過屏幕抓取足球結果並將其保存到模型(結果),目前爲止確定....我在模型之間建立了一些關聯,我想從相關模型中獲取所有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