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