# Delegates and building associations

**URL:** <https://www.ruby-forum.com/t/delegates-and-building-associations/178906>\
**Category:** Rails\
**Created:** [November 13, 2009, 10:35am UTC](https://www.ruby-forum.com/t/delegates-and-building-associations/178906 "2009-11-13T10:35:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![twiggle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@twiggle](https://www.ruby-forum.com/u/twiggle)\
**Post date:** [November 13, 2009, 10:35am UTC](https://www.ruby-forum.com/t/delegates-and-building-associations/178906/1 "2009-11-13T10:35:24Z")

</div>

Class User \<\< ActiveRecord::Base  
has\_one :profile  
delgate :description, :description=, :to =\> :profile

end

I’d like to do,

@user = User.create(:name =\> ‘Foo B.’, :description =\> ‘Slow.’)  
or  
@user = User.update\_attributes(:description =\> ‘Slow’)

and have AR create/update the profile model and association (if it  
doesn’t already exist).

How can I do this? Ideally this would be handled in User’s  
initialize, or callback, but I can’t get anything to work. When  
reaching the delegate statement I get an error that profile is nil.

Thanks

---

<div class="post-metadata">

**Author:** ![twiggle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ad7895/32.png) [@twiggle](https://www.ruby-forum.com/u/twiggle)\
**Post date:** [November 13, 2009, 1:05pm UTC](https://www.ruby-forum.com/t/delegates-and-building-associations/178906/2 "2009-11-13T13:05:17Z")

</div>

Write Something like this in your user model  
before\_create :build\_profile\_for\_this\_user

def build\_proile\_for\_this\_user  
build\_profile  
end
