Debugging why a create method is not saving to the model

ε祈祈猫儿з 提交于 2019-12-12 01:15:13

问题


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


回答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.



来源:https://stackoverflow.com/questions/15386450/debugging-why-a-create-method-is-not-saving-to-the-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!