Can't save to the database?

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!

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. [email protected]

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. [email protected]

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!