Debugging why a create method is not saving to the model
Asked Answered
R

1

0

I have this method which after a rake task should save my data to the model, I am trying to update 2 columns at a time, which shouldnt be a problem

  def update_fixtures #rake task method
   Fixture.destroy_all
   get_home_fixtures.each {|home| Fixture.create!(home_team: home )}
   get_away_fixtures.each {|away| Fixture.create!(away_team: away )}
  end

In this instance only the away team will save, however if i do this

  def update_fixtures #rake task method
     Fixture.destroy_all
     get_away_fixtures.each {|away| Fixture.create!(away_team: away )}
     get_home_fixtures.each {|home| Fixture.create!(home_team: home )}
  end

Then only the home team will save.

I have tried @model.errors.full_messages but get undefined method for nil class, so there are none?

I am trying to debug this issue but unsure on what i can use to find the problem

Has anyone experienced this before? Driving me insane

EDIT

I can update the field manually from the console, so when I do

Fixture.create(:away_team => "String")

it updates fine

Thanks

Renaldorenard answered 13/3, 2013 at 13:17 Comment(0)
O
1

As you're using #create!, it should throw an error if it failed to save, which should mean that you're not trying to save anything (empty array).

Verify that

get_away_fixtures
get_home_fixtures

look as you expect them to. Install debugger and add it after Fixture.destroy_all.

Oma answered 13/3, 2013 at 14:35 Comment(5)
where will the error be produced, as you say create! should throw an errorRenaldorenard
When the method is called. You're running some rake task that calls that method, right? When that task calls the method the error should abort the task and show a stack trace.Oma
ah right, no it doesn't do that, it completes the rake task and fills in either the home team or away team info, dependent upon which way around i have my methods, ie get_home_fixturesRenaldorenard
I'd still recommend throwing the debugger in there, checking what get_away_fixtures and get_home_fixtures contains and running the rest of the method manually.Oma
Ive accepted answer as debugger is a nice tool, with regards as to why both methods where not writing to the model, its because I have found that one create was overridding the other, which makes sense nowRenaldorenard

© 2022 - 2024 — McMap. All rights reserved.