can't dup Fixnum

I know this mst be a newbie error, but here goes: I have two classes

class User < ActiveRecord::Base    has_one :associate end

and

class Associate < ActiveRecord::Base   belongs_to :user end

These are one-to-one. In other words, Associate has one User and vice versa, and they Associate.id=User.associate_id and Associate.user_id=User.id

I go to create a new User....I want the Associate to be created automatically (in fact, duplicating the username and email field, and putting in a start date). I want to update the User.associate_id and the Associate.user_id.

What follows is my UsersController for doing this, which is throwing

class UsersController < ApplicationController   def new     @user = User.new   end

  def create     @user = User.new(params[:user])     @user.associate_id=0     associate = Associate.new()     associate.email=@user.email     associate.username=@user.login     associate.startdate=Date.today     if @user.save       associate.user_id=@user.id       associate.save       @user.associate_id=associate.id       @user.update_attributes(@user.associate_id)       flash[:notice] = "Registration successful."       redirect_to root_url     else       render :action => 'new'     end   end ... end

and here is the log that gets created:

  e[4;36;1mSQL (0.0ms)e[0m e[0;1mSET SQL_AUTO_IS_NULL=0e[0m

Processing UsersController#create (for 127.0.0.1 at 2009-05-21 15:00:48) [POST]   Parameters: {"user"=>{"password_confirmation"=>"[FILTERED]", "password"=>"[FILTERED]", "login"=>"joey", "email"=>"joey@xyz.net"}, "commit"=>"Submit", "action"=>"create", "authenticity_token"=>"8AAkYRFmlpenJdd6ZuTd1b4G1aAiUB40pXCn9syINsw=", "controller"=>"users"}   e[4;35;1mUser Columns (16.0ms)e[0m e[0mSHOW FIELDS FROM `users`e [0m   e[4;36;1mAssociate Columns (16.0ms)e[0m e[0;1mSHOW FIELDS FROM `associates`e[0m   e[4;35;1mSQL (0.0ms)e[0m e[0mBEGINe[0m   e[4;36;1mUser Exists (0.0ms)e[0m e[0;1mSELECT `users`.id FROM `users` WHERE (LOWER(`users`.`email`) = BINARY 'joey@xyz.net') LIMIT 1e [0m   e[4;35;1mUser Exists (0.0ms)e[0m e[0mSELECT `users`.id FROM `users` WHERE (LOWER(`users`.`login`) = BINARY 'joey') LIMIT 1e[0m   e[4;36;1mUser Exists (0.0ms)e[0m e[0;1mSELECT `users`.id FROM `users` WHERE (`users`.`persistence_token` = BINARY 'ad2f35e2ba9354a359386fd85597b4b3bfe6709172776a7bcbd01643b77b23fa5531daaa2eaa41fe81899f0c5f6f0013eb09701a2402b5305902af5a113bb0df') LIMIT 1e[0m   e[4;35;1mUser Load (0.0ms)e[0m e[0mSELECT * FROM `users` WHERE (`users`.`persistence_token` = '84defa206a76ccc30e4b768283b4da1590f06b90ed5c8091a81aa62dfbdffcb87066135e257325259f8c37c8318da771b6aecb22dafe8c291271e9efa9f60806') LIMIT 1e[0m   e[4;36;1mCACHE (0.0ms)e[0m e[0;1mSELECT * FROM `users` WHERE (`users`.`persistence_token` = '84defa206a76ccc30e4b768283b4da1590f06b90ed5c8091a81aa62dfbdffcb87066135e257325259f8c37c8318da771b6aecb22dafe8c291271e9efa9f60806') LIMIT 1e[0m   e[4;35;1mUser Create (0.0ms)e[0m e[0mINSERT INTO `users` (`last_login_at`, `updated_at`, `last_request_at`, `crypted_password`, `current_login_ip`, `password_salt`, `current_login_at`, `persistence_token`, `login_count`, `login`, `created_at`, `email`, `associate_id`, `last_login_ip`) VALUES(NULL, '2009-05-21 19:00:49', '2009-05-21 19:00:49', 'cc42cb5dd198622ad51de4144a2cfed0f86ded721c9d6306f2ffebcb498395e31e131aacd66cc2b36649dffd5c028c13315a6f33ed896790ac2b7eb816328fe2', '127.0.0.1', 'fkBwzEfpTdtlut7302gI', '2009-05-21 19:00:49', 'ad2f35e2ba9354a359386fd85597b4b3bfe6709172776a7bcbd01643b77b23fa5531daaa2eaa41fe81899f0c5f6f0013eb09701a2402b5305902af5a113bb0df', 1, 'joey', '2009-05-21 19:00:49', 'joey@xyz.net', 0, NULL)e[0m   e[4;36;1mSQL (0.0ms)e[0m e[0;1mCOMMITe[0m   e[4;35;1mSQL (0.0ms)e[0m e[0mBEGINe[0m   e[4;36;1mAssociate Create (0.0ms)e[0m e[0;1mINSERT INTO `associates` (`prefix`, `city`, `dob`, `ssno`, `phonework`, `street1`, `username`, `middlename`, `street2`, `country`, `custom1`, `firstname`, `lastname`, `inoutexplanation`, `custom2`, `notes`, `crossid`, `postalcode`, `phonehome`, `phoneother`, `custom3`, `branch`, `custom4`, `deleted`, `phonefax`, `organization`, `user_id`, `tstamp`, `phonecell`, `jobtitle`, `startdate`, `inoutstatus`, `state`, `email`, `maritalstatus`) VALUES(NULL, NULL, NULL, NULL, NULL, NULL, 'joey', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, 1, NULL, NULL, NULL, '2009-05-21', NULL, NULL, 'joey@xyz.net', NULL)e[0m   e[4;35;1mSQL (0.0ms)e[0m e[0mCOMMITe[0m

TypeError (can't dup Fixnum):   app/controllers/users_controller.rb:17:in `create'

Rendered rescues/_trace (109.0ms) Rendered rescues/_request_and_response (0.0ms) Rendering rescues/layout (internal_server_error)

I have not examined the trace in detail but I don’t believe you should have an associate_id column in the users table. If you think about it, it is redundant. Rails will expect a user_id column in the associates table and will use that to make the connection both ways.

Colin

Assuming that this is line 17: @user.update_attributes(@user.associate_id)

You need to look at what the #update_attributes method really does. The argument should be a hash of attribute=>value pairs.

Also, for the association has_one/belongs_to

User#id <==> Associate#user_id

There is no foreign key in the User model. It will find the Associate with something similar to:

SELECT associates.* FROM associates WHERE associates.user_id = #{user.id} LIMIT 1

when you do user.associate in your code.

-Rob

Rob Biedenharn http://agileconsultingllc.com

Rob@AgileConsultingLLC.com