Hello Bradley,
Please check on this railscast
http://railscasts.com/episodes/189-embedded-association
This is a better way to handle roles in your case.
[]''s
Rodrigo Dellacqua
IBM - IGF Project Manager
"Communication is the key"
On Tue, Dec 29, 2009 at 11:46 PM, Bradley
<bradleyrobertson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:
> 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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
> To unsubscribe from this group, send email to
>
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
> .
> For more options, visit this group at
> 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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.