ruby on rails - Grab id from another model and then save it into a new model -


really trying head around following situation. grabbing football results via screen scrape , saving them model (result), ok far.... have associations setup between models , want grab ids associated model , save result model.. models setup so

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 

my screen scrape looks so

def get_results   doc = nokogiri::html(open(results_url))  days = doc.css('.table-header').each |h2_tag|   date = date.parse(h2_tag.text.strip).to_date    matches = h2_tag.xpath('following-sibling::*[1]').css('tr.report')    matches.each |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 

so before create result need prediction id's fixture model correspond correct result (football match) , save them @ same time when save other attributes..i hope makes sense..

thanks

edit

ok have got far

fixture = fixture.where(fixture_date: date, home_team: home_team, away_team: away_team).first prediction_array = prediction.where(fixture_id: fixture.id) 

need pull out values then..

currently have prediction id in fixture model, means each fixture can have 1 prediction.

also, couple of attributes redundant - if prediction references fixture doesn't need store it's own information on teams.

i suggest removing results model , altering other 2 models following:

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} , away_score = #{self.away_score}"]} end  class prediction < activerecord::base   attr_accessible :fixture_id, :home_score, :away_score    belongs_to :fixture end 

now instead of creating result entry, find fixture fixture.where(home_team: home_team, away_team: away_team, fixture_date: date) , set 2 scores.

correct_predictions association condition, once fixture has scores filled in can call my_fixture.correct_predictions of correct predictions (you might need change condition depending on database adapter).


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -