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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Amos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> 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! > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nicholas Henry 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 Amos <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>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! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---