# Creating an updateable ActiveRecord object for an existing r

**URL:** <https://www.ruby-forum.com/t/creating-an-updateable-activerecord-object-for-an-existing-r/71477>\
**Category:** Rails\
**Created:** [October 9, 2006, 9:53am UTC](https://www.ruby-forum.com/t/creating-an-updateable-activerecord-object-for-an-existing-r/71477 "2006-10-09T09:53:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 9, 2006, 9:53am UTC](https://www.ruby-forum.com/t/creating-an-updateable-activerecord-object-for-an-existing-r/71477/1 "2006-10-09T09:53:15Z")

</div>

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 H.

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 9, 2006, 10:23am UTC](https://www.ruby-forum.com/t/creating-an-updateable-activerecord-object-for-an-existing-r/71477/2 "2006-10-09T10:23:19Z")

</div>

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

Am 09.10.2006 um 09:50 schrieb Chris H.:

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 9, 2006, 10:27am UTC](https://www.ruby-forum.com/t/creating-an-updateable-activerecord-object-for-an-existing-r/71477/3 "2006-10-09T10:27:20Z")

</div>

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

Am 09.10.2006 um 09:50 schrieb Chris H.:

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [October 9, 2006, 10:27am UTC](https://www.ruby-forum.com/t/creating-an-updateable-activerecord-object-for-an-existing-r/71477/4 "2006-10-09T10:27:20Z")

</div>

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!
