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
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...
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
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:
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.
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.