# Nested one-to-one forms question

**URL:** <https://www.ruby-forum.com/t/nested-one-to-one-forms-question/186628>\
**Category:** Rails\
**Created:** [April 10, 2010, 12:03am UTC](https://www.ruby-forum.com/t/nested-one-to-one-forms-question/186628 "2010-04-10T00:03:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [April 10, 2010, 12:03am UTC](https://www.ruby-forum.com/t/nested-one-to-one-forms-question/186628/1 "2010-04-10T00:03:58Z")

</div>

Hello, I must admit I’m new to rails (I actually come from the .net  
world, but I’ve been meaning to expand for some time now)

I’m trying to create a user registration page (just getting my feet wet  
really) and I’ve hit a wall. I’m pretty sure I’m doing this wrong, so I  
need some straightening out 🙂

My db looks something like this:

UserAccount: Name, Email, Website, Status  
Password: Key, Question, Answer

and my models:

class UserAccount \< ActiveRecord::Base  
validates\_presence\_of :Name, :Email  
validates\_uniqueness\_of :Name, :Email

has\_one :Password

accepts\_nested\_attributes\_for :Password  
end

class Password \< ActiveRecord::Base  
belongs\_to :user\_account

attr\_accessor :password\_confirmation  
validates\_confirmation\_of :password  
end

and the form that I’m trying to produce:

\<% form\_for(@user\_account) do |f| %\>  
\<%= f.error\_messages %\>

 Required

```
<p>
  <%= f.label :Name %><br />
  <%= f.text_field :Name %>
</p>

<% f.fields_for :Password do |fp| %>
  <p>
    <%= fp.label :Password %><br />
    <%= fp.password_field :key %>
  </p>
  <p>
    <%= fp.label :password_confirmation %><br />
    <%= fp.password_field :password_confirmation %>
  </p>
<% end %>
<p>
  <%= f.label :Email %><br />
  <%= f.text_field :Email %>
</p>

```

 Optional 

\<%= f.label :Website %\>  
 \<%= f.text\_field :Website %\>

\<%= f.label :Status %\>  
 \<%= f.text\_area :Status %\>

\<%= f.submit 'Create' %\>

\<% end %\>

Now, as is, the browser doesn’t render the password at all. I think  
that it is because “fields\_for” is expecting a one-to-many relationship  
and finding a one-to-one relationship (?), but if I take out the line  
accepts\_nested\_attributes\_for :Password I get the following error:

ActiveRecord::AssociationTypeMismatch in User accountsController#create  
Password(#57105720) expected, got HashWithIndifferentAccess(#30298510)

I’m really stuck and I probably look really dumb, so that’s why i’m here  
🙂

Thank you for ANY advice and sorry for my poor design/implementation

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [April 10, 2010, 12:08am UTC](https://www.ruby-forum.com/t/nested-one-to-one-forms-question/186628/2 "2010-04-10T00:08:53Z")

</div>

whoa, I guess I better explain what I’m trying to do.

What I want to do is be able to create a User and a Password object with  
the same form. I’m going to add the question part later (once this is  
done)

I figure that since User has a single instance of a Password, I could  
gain access to it just like an attribute, and render the text\_fields  
with :Password.Key or :Password.Question, but that doesn’t work either.

So if I can be shown how to edit 2 models (or just a model and its sub  
model) with the same form, that would be excellent

Thanks!

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [April 10, 2010, 3:00am UTC](https://www.ruby-forum.com/t/nested-one-to-one-forms-question/186628/3 "2010-04-10T03:00:34Z")

</div>

Joel Day wrote:

> whoa, I guess I better explain what I’m trying to do.
> 
> What I want to do is be able to create a User and a Password object with  
> the same form. I’m going to add the question part later (once this is  
> done)
> 
> I figure that since User has a single instance of a Password, I could  
> gain access to it just like an attribute, and render the text\_fields  
> with :Password.Key or :Password.Question, but that doesn’t work either.
> 
> So if I can be shown how to edit 2 models (or just a model and its sub  
> model) with the same form, that would be excellent
> 
> Thanks!

Basically in your controller have the new action create 2 new instances:

@user\_account = User.new  
@user\_account.password.build

form\_new needs these new instances, specially the password one. That is  
why its not rendering it.
