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

**URL:** <https://www.ruby-forum.com/t/ideas-why-this-rake-task-isnt-saving-one-of-my-models/171988>\
**Category:** Rails\
**Created:** [July 13, 2009, 2:11am UTC](https://www.ruby-forum.com/t/ideas-why-this-rake-task-isnt-saving-one-of-my-models/171988 "2009-07-13T02:11:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 13, 2009, 2:11am UTC](https://www.ruby-forum.com/t/ideas-why-this-rake-task-isnt-saving-one-of-my-models/171988/1 "2009-07-13T02:11:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 13, 2009, 3:51am UTC](https://www.ruby-forum.com/t/ideas-why-this-rake-task-isnt-saving-one-of-my-models/171988/2 "2009-07-13T03:51:00Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 13, 2009, 4:02am UTC](https://www.ruby-forum.com/t/ideas-why-this-rake-task-isnt-saving-one-of-my-models/171988/3 "2009-07-13T04:02:42Z")

</div>

On Sun, Jul 12, 2009 at 9:50 PM, JangoSteve[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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:)

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 13, 2009, 4:14am UTC](https://www.ruby-forum.com/t/ideas-why-this-rake-task-isnt-saving-one-of-my-models/171988/4 "2009-07-13T04:14:58Z")

</div>

On Jul 13, 3:02 am, Rick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sun, Jul 12, 2009 at 9:50 PM, JangoSteve[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![rick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@rick](https://www.ruby-forum.com/u/rick)\
**Post date:** [July 13, 2009, 4:17am UTC](https://www.ruby-forum.com/t/ideas-why-this-rake-task-isnt-saving-one-of-my-models/171988/5 "2009-07-13T04:17:54Z")

</div>

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[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
