Ideas why this rake task isn't saving one of my models?

Newbie here working on an application that has a “Match” model and
each Match can have some “Playergame” models. The rails apps is
actually working, but I needed to now import some initial data from a
csv file, so I built a rake task to handle the import.

The problem is that for some reason I can’t seem to get “Match” to
save. It doesn’t throw any errors, but after I run it and I go to the
ruby console and do Match.find(:all) it returns empty. The rake task
can save Playergames just find and I’ll see them if I do
Playergame.find(:all)

In the below script below I have the Playergame additions commented
out because I simply wanted to see if I can get a Match to save alone
(which it doesn’t)

Any help appreciated:

#striped down script

task(:cvsimport => :environment) do

FasterCSV.foreach("#{RAILS_ROOT}/hockey.csv") do |row|

  rick_game = Playergame.new
  steve_game = Playergame.new
  match = Match.new

  #set a bunch of fields reading over the row
  #left out for brevity

  #match.playergames << rick_game
  #match.playergames << steve_game

  #rick_game.save  #this will save a game
  #steve_game.save  #this will save a game

  #why is match not be saving???????????
  match.save

end

#models

class Match < ActiveRecord::Base
has_many :playergames, :dependent => :destroy
accepts_nested_attributes_for :playergames
end

class Playergame < ActiveRecord::Base
belongs_to :match, :foreign_key => “match_id”
end

I’m not sure this is what is causing your problem, but according to
your models, a Match has_many Playergames, which would mean that each
Playergame has a match_id, correct? However, in your script, you are
saving the Playergames before the Match, meaning a match_id has not
yet been created. When you do Playergame.find(:all), I’m assuming they
show match_id=nil. Anyway, I’d at least try changing your script to
save Match before Playergames, and see if you’re still having
problems…

  #match.playergames << rick_game
  #match.playergames << steve_game

  match.save

  #rick_game.save  #this will save a game
  #steve_game.save  #this will save a game

On Sun, Jul 12, 2009 at 9:50 PM, JangoSteve[email protected]
wrote:

 #match.playergames << rick_game
 #match.playergames << steve_game

 match.save

 #rick_game.save  #this will save a game
 #steve_game.save  #this will save a game

Good point, about saving the games first, but I’m curious, what the
heck is going on that I can’t seem to get match to save even with
those game save lines commented out (as above.) I’m just trying to get
a Match to save.

If I call match.save and it doesn’t save shouldn’t the script throw an
error? I guess that’s why I’m a little stumped since the script seems
to fun just fine.

When I use the rails app UI to add match it seems to save just fine as
you can see by a quick call in the console:

Match.find(:all)
=> [#<Match id: 1, match_date: “2009-07-12 00:00:00”, overtime: false,
shootout: false, linechanges: true, period_length: 20, game_version:
nil, notes: nil>]

I’m sure this is going to turn out to be something really stupid, so
I’m ready for the ‘duh’ moment to hit me:)

On Jul 13, 3:02 am, Rick [email protected] wrote:

On Sun, Jul 12, 2009 at 9:50 PM, JangoSteve[email protected] wrote:

Good point, about saving the games first, but I’m curious, what the
heck is going on that I can’t seem to get match to save even with
those game save lines commented out (as above.) I’m just trying to get
a Match to save.

If I call match.save and it doesn’t save shouldn’t the script throw an
error? I guess that’s why I’m a little stumped since the script seems
to fun just fine.

save just returns true/false. save! throws an error

One way of getting really confusing results is accidentally
overwriting core active record methods (though from what you have
shown so far you haven’t)

Other than that, there’s always stepping through it with the debugger
until you understand what’s up.

Fred

Ah thanks Frederick. I’ll start using save!

And with that, I figured it out. I had a validates_presence_of
condition that wasn’t getting met. Yea a duh moment. I just figured
something like would have thrown an error if there was an issue in my
validate conditions. I’m glad I know about save! now. thanks.

On Sun, Jul 12, 2009 at 10:14 PM, Frederick
Cheung[email protected] wrote:

error? I guess that’s why I’m a little stumped since the script seems

I’m sure this is going to turn out to be something really stupid, so
I’m ready for the ‘duh’ moment to hit me:)


Rick R