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