2013-05-14 62 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

相关问题