真的试图了解以下情况。我通过屏幕抓取抓住足球结果并将它们保存到模型(结果),到目前为止确定....我在模型之间设置了一些关联,我想从相关模型中获取所有ID并保存到我的结果中model ..我的模型设置如此
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)
需要提取值然后..
答案 0 :(得分: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
来获得所有正确的预测(你可能需要根据你的数据库改变条件)适配器)。
答案 1 :(得分: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
所以从抓取的结果中获取与date,home_team,away_team匹配的所有灯具。然后获取fixture_id与fixture所接收的id匹配的所有预测。
然后将它们映射出来,遍历它们并将其值赋给pred_id
答案 2 :(得分:-1)
如果您有预测belongs_to Fixture关联,则可以执行:fixture.prediction