Creating an updateable ActiveRecord object for an existing record

Folks,

Can someone please suggest why the following code does not work?

class User < ActiveRecord::Base

def register   user = User.find_by_email_address(self.email_address)   if !user.nil?     self.id = user.id   end   self.registered = 1   self.save end

This is for a user registration system, and the intention is that if the user does not exist then it will be newly created, otherwise if a placeholder account already exists (e.g. the user has been invited into the system by a friend but has not yet accepted) then the placeholder account will be updated.

I could do this in the controller, but this seems much cleaner. It is unclear to me how to "pick up" the existing DB record, so that saving updates the existing record. I thought assigning the primary key to the existing record would suffice, but this does not appear to work.

Any help is greatly appreciated.

Regards, Chris Hughes

Why do you try to overwrite the id?

if you have something in your controller that looks like: @user = User.find_by_something_and_confirmation_code params [:something], 'INSERT_CONFIRMATION_CODE_HERE' @user and @user.register

you can put something in the Modul like: def register    self.update_attribute :registered, true end

Regards Florian

I was creating a new User object in my controller, just based on params[:user], and doing validation on that before attempting the registration code.

But you're right, I might as well do it all at once.

Thanks!

Why do you try to overwrite the id?

if you have something in your controller that looks like: @user = User.find_by_something_and_confirmation_code params [:something], 'INSERT_CONFIRMATION_CODE_HERE' @user.register

you can put something in the Modul like: def register    self.update_attribute :registered, true end