Forum: Ruby on Rails Validate_presence_of error in nested object is not displayed

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ovidiu EFTIMIE (Guest)
on 2006-04-07 12:07
(Received via mailing list)
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
Wiebe C. (Guest)
on 2006-04-07 15:01
(Received via mailing list)
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.
This topic is locked and can not be replied to.