# Can't save to the database?

**URL:** <https://www.ruby-forum.com/t/cant-save-to-the-database/124674>\
**Category:** Rails\
**Created:** [January 1, 2008, 12:25am UTC](https://www.ruby-forum.com/t/cant-save-to-the-database/124674 "2008-01-01T00:25:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [January 1, 2008, 12:25am UTC](https://www.ruby-forum.com/t/cant-save-to-the-database/124674/1 "2008-01-01T00:25:14Z")

</div>

Hi everyone,

I’m probably doing something dumb, but I’m trying to save some guesses  
to the guesses table in the database, but it doesn’t work. I’m taking in  
several guesses at once, then looping through each of them and saving  
them separately. Here’s my controller code for the create action in my  
guesses\_controller:

def create  
params.keys.select {|k| /home\_score/.match(k)}.each do  
|homescore\_key|

```
      game_id = homescore_key.scan(/\d+/).first.to_i
      homescore_val = params[homescore_key]
      visitingscore_key = "away_score_#{game_id}"
      visitingscore_val = params[visitingscore_key]
      cuser = params[:user_id]
      gameweek_id = params[:gameweek_id]

      guess = Guess.new(:game_id => game_id, :home_score =>

```

homescore\_val, :away\_score =\> visitingscore\_val, :user\_id =\> cuser,  
:gameweek\_id =\> gameweek\_id)  
guess.save  
end  
end

Now, all of the variables I’m giving to Guess.new are valid and have  
what I want in them, but it won’t save it to the database. Here’s what I  
get when I inspect guess right before guess.save:

#\<Guess id: nil, home\_score: 1, away\_score: 1, multiplier: false,  
points: nil, user\_id: 1, game\_id: 1, gameweek\_id: 1, created\_at: nil,  
updated\_at: nil\>

Any ideas? It seems like saving it to the database should be an easy  
task! I’m probably missing something obvious. Thanks!

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [January 1, 2008, 1:49am UTC](https://www.ruby-forum.com/t/cant-save-to-the-database/124674/2 "2008-01-01T01:49:27Z")

</div>

Dave:

I didn’t take much time to look at this, but maybe try using create!  
or save! - it should throw an exception if it doesn’t save - right now  
it’s just silently failing. e.g.

```
      guess = Guess.create!(:game_id => game_id, :home_score =>

```

homescore\_val, :away\_score =\> visitingscore\_val, :user\_id =\> cuser,  
:gameweek\_id =\> gameweek\_id)

HTH,  
Nicholas

On Dec 31, 6:25 pm, Dave A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![daveamos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@daveamos](https://www.ruby-forum.com/u/daveamos)\
**Post date:** [January 2, 2008, 12:35am UTC](https://www.ruby-forum.com/t/cant-save-to-the-database/124674/3 "2008-01-02T00:35:01Z")

</div>

Nicholas H. wrote:

> Dave:
> 
> I didn’t take much time to look at this, but maybe try using create!  
> or save! - it should throw an exception if it doesn’t save - right now  
> it’s just silently failing. e.g.
> 
> ```
> guess = Guess.create!(:game_id => game_id, :home_score =>
> 
> ```
> 
> homescore\_val, :away\_score =\> visitingscore\_val, :user\_id =\> cuser,  
> :gameweek\_id =\> gameweek\_id)
> 
> HTH,  
> Nicholas
> 
> On Dec 31, 6:25 pm, Dave A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks for the tip! It turned out that it was failing a validation, but  
I wouldn’t have seen that without the save! . Thanks again!
