# Error using nested attributes

**URL:** <https://www.ruby-forum.com/t/error-using-nested-attributes/193960>\
**Category:** Rails\
**Created:** [August 27, 2010, 6:14am UTC](https://www.ruby-forum.com/t/error-using-nested-attributes/193960 "2010-08-27T06:14:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [August 27, 2010, 6:14am UTC](https://www.ruby-forum.com/t/error-using-nested-attributes/193960/1 "2010-08-27T06:14:24Z")

</div>

I have following models

class User \< ActiveRecord::Base

belongs\_to :user\_type  
has\_many :addresses, :as =\>:addressee, :dependent =\> :destroy  
has\_many :photos,:as =\> :asset,:dependent =\> :destroy

accepts\_nested\_attributes\_for :addresses, :allow\_destroy =\> true  
accepts\_nested\_attributes\_for :photos, :allow\_destroy =\> true  
end

class Photo \< ActiveRecord::Base

belongs\_to :asset, :polymorphic =\> true

validates\_attachment\_presence :photo unless :photo  
validates\_attachment\_content\_type :photo, :content\_type =\>  
[‘image/jpeg’, ‘image/jpg’, ‘image/gif’, ‘image/png’, ‘image/bmp’]  
unless :photo  
attr\_accessor :photo

# Paperclip

has\_attached\_file :photo, :styles =\> { :medium =\> “300x300\>”, :thumb  
=\> “100x100\>” }  
end

class Address \< ActiveRecord::Base

belongs\_to :addressee, :polymorphic =\> true

validates\_presence\_of :Address1, :maximum =\> 50  
validates\_presence\_of :City  
validates\_presence\_of :State  
validates\_presence\_of :ZipCode  
validates\_presence\_of :Country  
end

i have one form having combination of all this field if i hit submit  
button without adding any data it show all validation from users  
addresses model but not showing photo model validations & hiding photo  
field from UI  
not getting how this is happening? i am using nested attributes

here is my form

# Sign up as a new user
\<%= error\_messages\_for :user %\> \<% @user.password = @user.password\_confirmation = nil %\> \<% form\_for @user,:url =\> users\_path,:html =\> { :multipart =\> true } do |f| -%\> \<%= f.hidden\_field :user\_type\_id,:value=\>'2' %\> 

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

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

\<%= f.label :password %\>  
\<%= f.password\_field :password %\>

\<%= f.label :password\_confirmation, 'Confirm Password' %\>  
\<%= f.password\_field :password\_confirmation %\>

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

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

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

 \<% f.fields\_for :photos do |fp| %\> 

\<%= fp.label :photo%\>  
\<%= fp.file\_field :photo%\>

 \<% end %\> \<% f.fields\_for :addresses do |fa| %\> 

\<%=fa.label :address\_1 %\>  
\<%=fa.text\_area :Address1,:cols =\> 30,:rows =\> 3 %\>

\<%=fa.label :address\_2 %\>  
\<%=fa.text\_area :Address2,:cols =\> 30,:rows =\> 3 %\>

\<%=fa.label :City %\>  
\<%=fa.text\_field :City %\>

\<%=fa.label :State %\>  
\<%=fa.text\_field :State %\>

\<%=fa.label :zip\_code %\>  
\<%=fa.text\_field :ZipCode %\>

\<%=fa.label :Country %\>  
\<%=fa.text\_field :Country %\>

 \<% end %\> 

\<%= submit\_tag 'Sign up' %\>

\<% end -%\>

in controller

def new  
@user = User.new  
@user.photos.build  
@user.addresses.build  
end

#Create new user.  
def create  
logout\_keeping\_session!  
@user = User.new(params[:user])  
if @user.save  
redirect\_back\_or\_default(’/’)  
flash[:notice] = “Thanks for signing up! We’re sending you an  
email with your activation code.”  
else  
flash[:error] = “Sorry!! There was an error while setting up the  
account. Please try again, or contact our admin (removed\_email\_address@domain.invalid).”  
render :action =\> ‘new’  
end  
end

---

<div class="post-metadata">

**Author:** ![sunfish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@sunfish](https://www.ruby-forum.com/u/sunfish)\
**Post date:** [October 6, 2010, 9:00am UTC](https://www.ruby-forum.com/t/error-using-nested-attributes/193960/2 "2010-10-06T09:00:33Z")

</div>

anybody have any idea on it?

Sunny B. wrote:

> I have following models
> 
> class User \< ActiveRecord::Base
> 
> belongs\_to :user\_type  
> has\_many :addresses, :as =\>:addressee, :dependent =\> :destroy  
> has\_many :photos,:as =\> :asset,:dependent =\> :destroy
> 
> accepts\_nested\_attributes\_for :addresses, :allow\_destroy =\> true  
> accepts\_nested\_attributes\_for :photos, :allow\_destroy =\> true  
> end
> 
> class Photo \< ActiveRecord::Base
