# Nested objects not propagating from view

**URL:** <https://www.ruby-forum.com/t/nested-objects-not-propagating-from-view/178534>\
**Category:** Rails\
**Created:** [November 6, 2009, 3:44am UTC](https://www.ruby-forum.com/t/nested-objects-not-propagating-from-view/178534 "2009-11-06T03:44:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd\_ASJacobs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f17d59/32.png) [@Todd\_ASJacobs](https://www.ruby-forum.com/u/Todd_ASJacobs)\
**Post date:** [November 6, 2009, 3:44am UTC](https://www.ruby-forum.com/t/nested-objects-not-propagating-from-view/178534/1 "2009-11-06T03:44:12Z")

</div>

I thought I had this fixed, but apparently not. It works okay from the  
console, but not from the view. I have the following:

# partial schema

create\_table “users”, :force =\> true do |t|  
t.string “login”, :null =\> false  
t.string “first\_name”  
t.string “last\_name”  
t.string “email”, :null =\> false  
t.string “password”, :null =\> false  
t.integer “contact\_id”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end  
create\_table “contacts”, :force =\> true do |t|  
t.string “company\_name”  
t.integer “address\_id”  
t.integer “phone\_areacode”, :limit =\> 3  
t.integer “phone\_prefix”, :limit =\> 3  
t.integer “phone\_suffix”, :limit =\> 4  
t.integer “phone\_extension”, :limit =\> 5  
t.string “website”  
t.integer “biztype\_id”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end  
create\_table “addresses”, :force =\> true do |t|  
t.string “street\_address1”  
t.string “street\_address2”  
t.string “city”  
t.integer “state\_id”  
t.integer “zipcode”  
t.integer “plus4”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

# relevant models

class User \< ActiveRecord::Base  
belongs\_to :contact  
has\_many :properties  
has\_many :notes, :through =\> :properties

```
accepts_nested_attributes_for :contact, {

```

:allow\_destroy =\> true,  
:reject\_if =\> :all\_blank  
}  
end  
class Contact \< ActiveRecord::Base  
has\_one :user  
belongs\_to :address  
belongs\_to :biztype  
accepts\_nested\_attributes\_for :address, {  
:allow\_destroy =\> true,  
:reject\_if =\> :all\_blank  
}  
end  
class Address \< ActiveRecord::Base  
has\_one :contact  
has\_one :property  
belongs\_to :state  
end

# controller

def new  
@user = User.new  
@user.build\_contact.build\_address

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @user }
end

```

end  
def edit  
@user = User.find(params[:id])  
end

When I run on the console, I can create a new user object with nested  
attributes like so:

> > user=User.new  
> > =\> #\<User id: nil, login: nil, first\_name: nil, last\_name: nil, email:  
> > nil, password: nil, contact\_id: nil, created\_at: nil, updated\_at: nil\>
> 
> > user.build\_contact.build\_address  
> > =\> #\<Address id: nil, street\_address1: nil, street\_address2: nil,  
> > city: nil, state\_id: nil, zipcode: nil, plus4: nil, created\_at: nil,  
> > updated\_at: nil\>
> 
> > user.login, user.password, user.email = ‘foo’, ‘bar’, ‘baz’  
> > =\> [“foo”, “bar”, “baz”]
> 
> > user.contact.company\_name, user.contact.address.city = ‘foobar’,  
> > ‘foobaz’  
> > =\> [“foobar”, “foobaz”]
> 
> > user.save  
> > =\> true
> 
> > p user, user.contact, user.contact.address  
> > #\<User id: 4, login: “foo”, first\_name: nil, last\_name: nil, email:  
> > “baz”, password: “bar”, contact\_id: 2, created\_at: “2009-11-06  
> > 02:33:41”, updated\_at: “2009-11-06 02:33:41”\>  
> > #\<Contact id: 2, company\_name: “foobar”, address\_id: 3,  
> > phone\_areacode: nil, phone\_prefix: nil, phone\_suffix: nil,  
> > phone\_extension: nil, website: nil, biztype\_id: nil, created\_at:  
> > “2009-11-06 02:33:41”, updated\_at: “2009-11-06 02:33:41”\>  
> > #\<Address id: 3, street\_address1: nil, street\_address2: nil, city:  
> > “foobaz”, state\_id: nil, zipcode: nil, plus4: nil, created\_at:  
> > “2009-11-06 02:33:41”, updated\_at: “2009-11-06 02:35:09”\>  
> > =\> nil

As you can see, it appears to work fine, but the following views for  
new.html.erb and edit.html.erb don’t:

# New account

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

```
<p> <%= f.label :login %>: <%= f.text_field :login %> </p>
<p> <%= f.label :first_name %>: <%= f.text_field :first_name %> </p>
<p> <%= f.label :last_name %>: <%= f.text_field :last_name %> </p>
<p> <%= f.label :email %>: <%= f.text_field :email %> </p>
<p> <%= f.label :password %>: <%= f.password_field :password %> </p>
<% fields_for(@contact) do |f| %>
  <%= f.error_messages %>
  <p> <%= f.label :company_name %>: <%= f.text_field :company_name 

```

%\>

  

\<%= f.label :Type\_of\_Organization %\>: \<%= f.text\_field  
:biztype\_id %\>

  
\<% fields\_for(@address) do |f| %\>  
\<%= f.error\_messages %\> 

\<%= f.label :street\_address1 %\>: \<%= f.text\_field :street\_address1 %\>

\<%= f.label :street\_address2 %\>: \<%= f.text\_field :street\_address2 %\>

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

\<%= f.label :state\_id %\>: \<%= f.text\_field :state\_id, :maxlength=\>2 %\>

\<%= f.label :zipcode %\>: \<%= f.text\_field :zipcode, :maxlength=\>5 %\>-\<%= f.text\_field :plus4, :maxlength=\>4 %\>

 \<% end %\> 

\<%= f.label :phone %\>: (\<%= f.text\_field :phone\_areacode, :maxlength=\>3 %\>) \<%= f.text\_field :phone\_prefix, :maxlength=\>3 %\>-\<%= f.text\_field :phone\_suffix, :maxlength=\>4 %\> Ext. \<%= f.text\_field :phone\_extension, :maxlength=\>5 %\>

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

 \<% end %\>

```
<p> <%= f.submit 'Create' %> </p>

```

\<% end %\>

\<%= link\_to ‘Back’, accounts\_path %\>

The view doesn’t create the additional table entries, nor does it allow  
me to edit anything but the unjoined data (e.g. @users, not  
@users.contact). Obviously I’m still missing something important here. I  
want to be able to both populate _and_ edit this content from  
similarly-nested views.

Why does this work from the console, but not from within the web app  
itself?

–  
“Oh, look: rocks!”  
– Doctor Who, “Destiny of the Daleks”
