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!