0

真的想弄清楚以下情况。我正在通过屏幕抓取抓取足球结果并将它们保存到模型(结果),到目前为止还可以......我在模型之间设置了一些关联,我想从关联模型中获取所有 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)

然后需要提取值..

4

3 回答 3

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以获得所有正确的预测(您可能需要根据您的数据库适配器更改条件)。

于 2013-05-14T11:25:37.973 回答
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 与fixture 接收到的id 相匹配。

然后将它们映射出来,遍历它们并将其值分配给 pred_id

于 2013-05-14T14:59:19.707 回答
-1

如果你有一个 Prediction belongs_to Fixture 关联,你可以这样做:fixture.prediction

于 2013-05-14T11:09:40.567 回答