【问题标题】:Grab id from another model and then save it into a new model从另一个模型中获取 id,然后将其保存到新模型中
【发布时间】:2013-05-14 08:04:56
【问题描述】:

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

然后需要提取值..

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 model associations


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

    【讨论】:

    • 感谢您的回答,我已经让它工作了,但现在是时候重构并引入 has_many 关联了,这样一个夹具就可以有很多预测......所以会听取您的建议,看看我会怎么做
    【解决方案2】:

    好的,这可能是错误的方法,但我已经设法让它工作,最终代码看起来像这样

     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
    

    因此,从抓取的结果中获取所有匹配日期、主队、客队的赛程。然后得到所有的预测,其中fixture_id 与fixture 接收到的id 相匹配。

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我的意思是你可以用一个 ;)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-31
      • 2015-02-27
      • 1970-01-01
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多