Chris Hughes
2006-Oct-09 07:50 UTC
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Florian Aßmann
2006-Oct-09 08:18 UTC
Re: Creating an updateable ActiveRecord object for an existing record
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 Hughes:> > 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 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Chris Hughes
2006-Oct-09 08:24 UTC
Re: Creating an updateable ActiveRecord object for an existing record
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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Florian Aßmann
2006-Oct-09 08:24 UTC
Re: Creating an updateable ActiveRecord object for an existing record
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 Hughes:> > 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 > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---