# Nested attributes and initialization

**URL:** <https://www.ruby-forum.com/t/nested-attributes-and-initialization/180851>\
**Category:** Rails\
**Created:** [December 30, 2009, 11:41am UTC](https://www.ruby-forum.com/t/nested-attributes-and-initialization/180851 "2009-12-30T11:41:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@bradley](https://www.ruby-forum.com/u/bradley)\
**Post date:** [December 30, 2009, 11:41am UTC](https://www.ruby-forum.com/t/nested-attributes-and-initialization/180851/1 "2009-12-30T11:41:10Z")

</div>

I have a user model that has a few different roles (say a client and  
an agent) The agent has some extra relationships not found in a  
client role (has\_one :agent\_profile, for instance)

I have an edit\_profile action for an agent that has these nested  
attributes in the form and the model  
accepts\_nested\_attributes\_for :agent\_profile:

\<% form\_for @agent, :url =\> agent\_path(@agent) do |form| %\>  
\<%= form.label :name %\>  
\<%= form.text\_field :name %\>

\<% form.fields\_for :agent\_profile do |a\_form| %\>  
\<%= a\_form.label :website, “Website Address:”%\>  
\<%= a\_form.text\_field :website %\>  
\<% end %\>  
\<% end %\>

If an agent\_profile object isn’t present however, when an agent edits  
their profile, they get the:

ActionView::TemplateError (You have a nil object when you didn’t  
expect it!  
The error occurred while evaluating nil.new\_record?

Is it common that I would have an after\_initialize method such as:

def after\_initialize  
self.build\_agent\_profile if self.role\_name == “agent” && !  
self.agent\_profile  
end

Seems a bit odd to me to do this for all relationships I have that  
accept nested attributes, but I can’t really think of a better way. I  
also don’t want to split the User model into two models because agents  
and clients log in/sign up in the same way.

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![bradley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/c37758/32.png) [@bradley](https://www.ruby-forum.com/u/bradley)\
**Post date:** [December 31, 2009, 1:49am UTC](https://www.ruby-forum.com/t/nested-attributes-and-initialization/180851/2 "2009-12-31T01:49:24Z")

</div>

Hello Bradley,

Please check on this railscast

> **[\#189 Embedded Association - RailsCasts](http://railscasts.com/episodes/189-embedded-association)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

This is a better way to handle roles in your case.

[]'s  
Rodrigo D.  
IBM - IGF Project Manager  
“Communication is the key”

On Tue, Dec 29, 2009 at 11:46 PM, Bradley  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> \<%= form.text\_field :name %\>  
> ActionView::TemplateError (You have a nil object when you didn’t  
> Seems a bit odd to me to do this for all relationships I have that  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
