# Validate\_presence\_of error in nested object is not displayed

**URL:** <https://www.ruby-forum.com/t/validate-presence-of-error-in-nested-object-is-not-displayed/57116>\
**Category:** Rails\
**Created:** [April 7, 2006, 10:07am UTC](https://www.ruby-forum.com/t/validate-presence-of-error-in-nested-object-is-not-displayed/57116 "2006-04-07T10:07:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [April 7, 2006, 10:07am UTC](https://www.ruby-forum.com/t/validate-presence-of-error-in-nested-object-is-not-displayed/57116/1 "2006-04-07T10:07:06Z")

</div>

Hi,  
I have a contact object . This contact has a address object

> > Contact  
> > class Contact \< ActiveRecord::Base  
> > has\_one :address  
> > validates\_presence\_of :first\_name ,:last\_name  
> > end

.\>\>\> Address  
class Address \< ActiveRecord::Base  
belongs\_to :contact  
validates\_presence\_of :address  
end  
Addres has the following fields ; address, phone, fax, email

In the contacts\_controller I have  
def create  
@contact = Contact.new(params[:contact])  
@address = Address.new(params[:address])  
@contact.address= @address  
@saved = @contact.save  
logger.debug @contact.address.errors  
if @saved  
list  
render :update do |page|  
page.replace\_html “table”, :partial=\>“table”  
page.replace\_html “pagination”, :partial=\>‘pagination’  
page.replace\_html “msgs”,"Contact successfully added ",:queue=\>‘end’  
page.visual\_effect :fade,‘operations’,:duration=\>0.5  
page.visual\_effect :appear,‘contentTable’  
page.visual\_effect :appear, ‘msgs’,:queue=\>‘end’  
page.visual\_effect :fade, “msgs”,:duration=\>2,:queue=\>‘end’  
end  
else  
render :update do |page|  
@form\_name =‘Adding contact’  
page.replace\_html ‘formContent’,:partial=\>‘form’  
end  
end  
end

The problem is that the contact is saved and there is no message  
allerting me that the addres is mandatory . The line logger.debug  
@contact.address.errors shows me that accualy there is an error while  
saving but it’s not displayed .  
What I have tried to do is to save the contact then save the address  
@saved = @contact.save  
@savedAddr = @address.save  
If I do this the error message is displayed, but I’ve also noticed  
that the save is happening in two transactions

Can anyone tell me how I can manage this ?

Thanx,  
Ovidiu

---

<div class="post-metadata">

**Author:** ![Ovidiu\_EFTIMIE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/b9bd4f/32.png) [@Ovidiu\_EFTIMIE](https://www.ruby-forum.com/u/Ovidiu_EFTIMIE)\
**Post date:** [April 7, 2006, 1:01pm UTC](https://www.ruby-forum.com/t/validate-presence-of-error-in-nested-object-is-not-displayed/57116/2 "2006-04-07T13:01:45Z")

</div>

On Friday 07 April 2006 10:06, Ovidiu EFTIMIE wrote:

> belongs\_to :contact  
> logger.debug @contact.address.errors  
> end  
> @contact.address.errors shows me that accualy there is an error while  
> Ovidiu  
> I think if you use the pre-save method hook of contact to see if it has  
> an  
> unsaved object and save it would work.

Also mind BTW, that when saving contact when the associated address is  
new will  
save the address, but when address is an existing altered object/record,  
then  
it will not be saved. You will have to take care of this in the pre-save  
hook  
as well. Or, in general cases, specify a custom association, for  
relations  
that a lot of objects have. The only problem is, is that the method you  
can  
check to see if a record is new (new\_record?) returns false on an  
altered  
object (by design). That’s the whole cause of this behaviour te begin  
with. I  
don’t understand why rails only saves new associated objects, and not  
altered  
ones.
