# Creating a profile with after\_create

**URL:** <https://www.ruby-forum.com/t/creating-a-profile-with-after-create/127367>\
**Category:** Rails\
**Created:** [January 28, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/creating-a-profile-with-after-create/127367 "2008-01-28T17:49:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [January 28, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/creating-a-profile-with-after-create/127367/1 "2008-01-28T17:49:03Z")

</div>

I have a User and a Profile model, and want to use the last one for  
storing some settings and preferences. I think i have the  
relationships right (User…has\_one :profile,  
Profile…belongs\_to :user) and a user\_id field in the profile\_table

What i would like to do is automatically create a profile after  
someone registers, using…  
after\_create :create\_profile  
…in the User model and in the same file…

def create\_profile  
profile.create!  
end

this doesn’t work, and self.profile.create…etc neither. What am i  
doing wrong here? Doesn’t profile (in profile.create!) refer to the-  
created-user.profile?

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [January 28, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/creating-a-profile-with-after-create/127367/2 "2008-01-28T17:55:35Z")

</div>

On Jan 28, 2008, at 11:48 AM, Peter wrote:

> def create\_profile  
> profile.create!  
> end
> 
> this doesn’t work, and self.profile.create…etc neither. What am i  
> doing wrong here? Doesn’t profile (in profile.create!) refer to the-  
> created-user.profile?

Here’s how this is handled in one of my current applications:

class User \< ActiveRecord::Base  
has\_one :preference, :dependent =\> :destroy  
after\_create :initialize\_preference

protected

# after create filter

def initialize\_preference  
(self.preference = Preference.default).save  
end

end

class Preference \< ActiveRecord::Base  
belongs\_to :user

def self.default  
new(DEFAULTS)  
end  
end

And the Preference::DEFAULTS is just a hash of attribute values.  
Preference.default is also used for anonymous users on the system. If  
you substitute “Profile” for “Preference”, I think you’ll get the  
behavior that you’re looking for.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![peter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/278dde/32.png) [@peter](https://www.ruby-forum.com/u/peter)\
**Post date:** [January 28, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/creating-a-profile-with-after-create/127367/3 "2008-01-28T18:53:29Z")

</div>

thanks, this seems to be working

def create\_profile  
(self.profile = Profile.new).save!  
end

although i have the feeling this could be written in a cleaner way…?

On Jan 28, 5:54 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
