List Info

Thread: Can't save to the database?




Can't save to the database?
country flaguser name
Germany
2007-12-31 17:25:14
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_#"
          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-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Can't save to the database?
user name
2007-12-31 18:49:13
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...andreas-s.net>
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_#"
>           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-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Can't save to the database?
country flaguser name
Germany
2008-01-01 17:35:01
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...andreas-s.net>

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-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )