2013-05-14 68 views
0

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

需要拉出值的話..

回答

-1

如果你有一個預測belongs_to的燈具協會,你可以這樣做:fixture.prediction

+0

,但我不....... – Richlewis 2013-05-14 11:21:13

+0

我的意思是,你可以使用一個;你的答案) – yoones 2013-05-14 11:43:25

1

目前你有一個預測ID我您的燈具模型,這意味着每個燈具只能有一個預測。

此外,一些屬性是多餘的 - 如果預測引用了燈具,那麼它不需要將它自己的信息存儲在球隊中。

我建議去掉結果模型和改變其他兩種模式爲以下內容:

class Fixture < ActiveRecord::Base 
    attr_accessible :away_team, :fixture_date, :home_team, :kickoff_time, :home_score, :away_score 

    has_many :predictions 
    has_many :correct_predictions, class_name: "Prediction", foreign_key: "fixture_id", conditions: proc{["home_score = #{self.home_score} AND away_score = #{self.away_score}"]} 
end 

class Prediction < ActiveRecord::Base 
    attr_accessible :fixture_id, :home_score, :away_score 

    belongs_to :fixture 
end 

現在,而不是創建一個結果條目,只找到Fixture.where(home_team: home_team, away_team: away_team, fixture_date: date)夾具,並設置兩個分數。

correct_predictions是符合條件的關聯,所以,一旦你的燈具,填補了你可以叫my_fixture.correct_predictions讓所有正確的預測分數(您可能需要更改取決於你的數據庫適配器上的條件)。

+0

,謝謝,我已經得到了它工作,但現在它的時間來重構和引入has_many協會,所以夾具可以有很多預測..我會接受你的建議,看看我怎麼去 – Richlewis 2013-05-14 19:28:06

0

好了,所以這可能是錯誤的方式做到這一點,但我設法得到這個工作,最終的代碼看起來像這樣

def get_results # Get me all 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) 
    fixture = Fixture.where(fixture_date: date, home_team: home_team, away_team: away_team).first 
    prediction_array = Prediction.where(fixture_id: fixture) 

    pred = prediction_array.map {|p| p.id} 
     pred.each do |p| 
     pred_id = p 
     Result.create!(home_team: home_team, away_team: away_team, fixture_date: date, home_score: home_score, away_score: away_score, prediction_id: pred_id) 
    end 
    end 
end 
end 

我加了這個

fixture = Fixture.where(fixture_date: date, home_team: home_team, away_team: away_team).first 
    prediction_array = Prediction.where(fixture_id: fixture) 

    pred = prediction_array.map {|p| p.id} 
     pred.each do |p| 
     pred_id = p 

因此,抓住所有匹配日期,home_team,away_team的燈具。然後獲得fixture_id與夾具收到的id相匹配的所有預測。

然後映射出來,遍歷它們並指派其值設置爲pred_id

相關問題