# Add a profile model with User model - Edit Problem

**URL:** <https://www.ruby-forum.com/t/add-a-profile-model-with-user-model-edit-problem/141975>\
**Category:** Rails\
**Created:** [July 2, 2008, 8:46am UTC](https://www.ruby-forum.com/t/add-a-profile-model-with-user-model-edit-problem/141975 "2008-07-02T08:46:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenillen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chenillen](https://www.ruby-forum.com/u/chenillen)\
**Post date:** [July 2, 2008, 8:46am UTC](https://www.ruby-forum.com/t/add-a-profile-model-with-user-model-edit-problem/141975/1 "2008-07-02T08:46:46Z")

</div>

Hi there,

I used restful authentication to generate the authenticated system,  
but i still want to add a profile model as a part of user model.

So between user & profile I use has\_one association to connect them.

Create - OK  
But when i want to edit someone’s profile, the data not update in sql.

my user model is like this:

has\_one :profile  
attr\_accessible :login, :email, :password, :password\_confirmation

def profile\_attributes=(profile\_attributes)  
profile\_attributes do |attributes|  
profile = profile.detect {|p| p.id == attributes[:id].to\_i}  
profile.attributes = attributes  
end  
end

My Profile model is like this:  
belongs\_to :user

The migration has the user\_id foreign\_key set up.

In the view/user/edit.html.erb, i wrote enclosed codes:

\<% form\_for :user, :url =\> user\_url(@user), :html =\> { :method  
=\> :put } do |f| %\>

Email:  
\<%= f.text\_field :email, :size =\> 60 %\>

 \<% fields\_for "user[profile\_attributes][]", @user.profile do | p\_form| %\> 

Name: \<%= p\_form.text\_field :name, :index =\> nil %\>

Gender: \<%= p\_form.text\_field :gender, :index =\> nil %\>

Birthday: \<%= p\_form.text\_field :birth\_on, :index =\> nil %\>

Place: \<%= p\_form.text\_field :place, :index =\> nil %\>

About Me: \<%= p\_form.text\_field :about\_me, :index =\> nil %\>

 \<%= p\_form.hidden\_field :id, :index =\> nil %\> \<% end %\>

\<%= submit\_tag ‘Update’ %\>  
\<% end %\>

When I click update button, error says: Couldn’t find Profile without  
an ID

Or Can’t mass-assign in a protected attributes in the development log.

Can anyone help me?

---

<div class="post-metadata">

**Author:** ![chenillen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chenillen](https://www.ruby-forum.com/u/chenillen)\
**Post date:** [July 3, 2008, 4:55am UTC](https://www.ruby-forum.com/t/add-a-profile-model-with-user-model-edit-problem/141975/2 "2008-07-03T04:55:56Z")

</div>

Can anyone help me?

---

<div class="post-metadata">

**Author:** ![chenillen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@chenillen](https://www.ruby-forum.com/u/chenillen)\
**Post date:** [July 7, 2008, 12:21am UTC](https://www.ruby-forum.com/t/add-a-profile-model-with-user-model-edit-problem/141975/3 "2008-07-07T00:21:47Z")

</div>

On 7/2/08, \*Barrier [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can anyone help me?

You need to iterate through the hash:

profile\_attributes do |attributes|

Change to:

profile\_attributes.each do |attributes|

Also, be sure to save the profile model.

–  
Rick O.  
[http://lighthouseapp.com](http://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)
