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 Jul 12, 8:11 pm, Rick <[email protected]> wrote:
> 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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to