html input strings to database column integers

Hello all,

I am pulling my hair out on this. It seems like it should be the easiest
thing in the world to do, but it's just escaping me.

I have a rails built form and but I keep getting this error when I
submit it:

"Mysql::Error: Column 'user_id' cannot be null: INSERT INTO `games`
(..."

I have a hidden field in my form that collects @user.id which is passed
by the controller.

<% form_for @game, :url => user_games_path(@user), :method => :post do

f> %>

  <%= f.hidden_field :user_id, :value => @user.id %>
  <div class="formfield">
    <%= f.label :my_user_empire %>
    <%= f.select :user_empire,
Game::SelectableEmpires.options_for_select %>
  </div>

  <div class="formfield">
    <%=f.submit "Begin Game" %>
  </div>
<% end %>

Controller:
def new
  @user = User.find(params[:user_id])
  @game = Game.new
end

And I am pretty sure the problem is b/c the HTML hidden field is sending
the database a string and the DB is expecting an integer.

I've tried this in the model:

before_validation :user_id

def user_id
  self.user_id.to_i
end

but I get the error:
"stack level too deep"

I would greatly appreciate any help. Thank you.

In your controller, may be you need to set the user object in games object.

def new
@user = User.find(params[:user_id])
@game = Game.new

@game.user = @user

end

In your current code, there is nothing in the @game that links it to @user.

If below hint does not solve your problem, please paste your Game model code (apps/model/game.rb) here.

I hope you have “belongs_to” or “has_one” relationship setup between game and user.

Regards,
Srikanth

Here is my games model.

I do have a belongs_to relationship with users and users has_one game.

The problem isn't with this relationship though. I am able to get
retrieve any information related to the @user, no problem. The problem
I am having is that in my form I have html inputs that need to post to
my database. But the database is looking for integers here and the form
outputs strings.

I need to know if there's an easy (or any) to have these values from my
form be converted to integers before_save. I thought that what I have
below with defining "user_id" as "self.user_id.to_i" would do it but I
get that stack error.

class Game < ActiveRecord::Base

  belongs_to :user

  before_validation :user_id
  before_validation :user_empire

  def user_id
    self.user_id.to_i
  end

  def user_empire
    self.user_empire.to_i
  end
end

Again my form:
<% form_for @game, :url => user_games_path(@user), :method => :post do

f> %>

  <%= f.hidden_field :user_id, :value => @user.id %>
  <div class="formfield">
    <%= f.label :my_user_empire %>
    <%= f.select :user_empire,
          Game::SelectableEmpires.options_for_select %>
  </div>

  <div class="formfield">
    <%=f.submit "Begin Game" %>
  </div>
<% end %>

Refer Section 7 here http://guides.rubyonrails.org/getting_started.html It gives an example similar to the problem you are trying to solve.

Don’t think you need to define your own method “user_id”, as active record would have already generated one based on DB schema.

Also, conversion between integer and string is too taken care by Active Record. From a web form, all the data are received in string form in the HTTP POST body, so I dont think the issue is with data type conversion.

Regards,
Srikanth

Thank you for your response.

So I've realized that the model is just not receiving anything from
these fields.

I added:
  validates_presence_of :user_id
  validates_presence_of :user_empire
  validates_presence_of :difficulty

and I get the error in the form:
  "User can't be blank
   User empire can't be blank"

In the development log it shows these being posted with the correct
names, I've double checked them in the DB and it all matches up. So why
is it not submitting what's in the input to the DB?

Also it says "User can't be Blank" when it should be user_id. I am
completely confused here.

I do have the belongs_to relationship with :users but am not nesting the
new game form. I don't have to still do the accepts_nested_attributes,
do I?

Can you please provide code of your Game controller?

Also, let me know whether you are getting the error during create or update of game or both?

Here is my games_controller:

class GamesController < ApplicationController
  def show
    @user = User.find(params[:user_id])
    @game = Game.find(params[:id])
  end

  def new
    @user = User.find(params[:user_id])
    @game = Game.new
  end

  def create
    @user = User.find(params[:user_id])
    @game = Game.new(params[:id])

    if @game.save
      redirect_to user_game_path(@user, @game)
    else
      render :action => 'new'
    end
  end

  def edit

  end

  def update

  end

  def destroy

  end
end

As you can see, I haven't even begun to fight with update or edit.
Updates to the games table will occur later with responses from a flash
document.

But Rails is not seeing them.

Processing UsersController#1 (for 127.0.0.1 at 2010-09-12 14:23:26)
[PUT]
  Parameters: {"user"=>{"game"=>{"num_of_enemies"=>"7",
"enemy_check_box"=>["0", "50", "0", "0", "30", "0", "0", "20", "0", "0",
"0"], "user_id"=>"1", "user_empire"=>"30", "difficulty"=>"10",
"selection_of_enemies"=>"2"}}, "commit"=>"Begin Game", "action"=>"1",
"_method"=>"put",
"authenticity_token"=>"SX7VfUzPm3SdzqFoKX4DQObQaHQLXSJudlNIfEGUt2Q=",
"id"=>"games", "controller"=>"users"}

In the “create” method, there is nothing that links “user” and “game” together…that is causing the issue.

I am little rusty on exact syntax, but based on my rails code I could refer, I suggest trying out followings:

Based on your logs, user_id is nested within user hash, so i suggest you use params[:user][:user_id] in User.find.

Also, use the in-built create method to create game instance from user instance (Refer http://guides.rubyonrails.org/association_basics.html#belongs_to-association-reference).

def create

@user = User.find(params**[:user][:user_id])
@game = @user
.create_game(params[:game])**

if @game.save
redirect_to user_game_path(@user, @game)
else
render :action => ‘new’
end
end

I am sorry…user_id is nested with game hash…so use params[:game][:user_id]

def create

@user = User.find(params[:game][:user_id])
@game = @user.create_game(params[:game])

if @game.save
redirect_to user_game_path(@user, @game)
else
render :action => ‘new’

end
end