# To edit a form with has\_many relation

**URL:** <https://www.ruby-forum.com/t/to-edit-a-form-with-has-many-relation/126064>\
**Category:** Rails\
**Created:** [January 15, 2008, 5:12am UTC](https://www.ruby-forum.com/t/to-edit-a-form-with-has-many-relation/126064 "2008-01-15T05:12:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![suneeta](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b77776/32.png) [@suneeta](https://www.ruby-forum.com/u/suneeta)\
**Post date:** [January 15, 2008, 5:12am UTC](https://www.ruby-forum.com/t/to-edit-a-form-with-has-many-relation/126064/1 "2008-01-15T05:12:54Z")

</div>

Hi…  
I have 2 tables contacts and contact\_addresses

In the model contact.rb I wrote:  
has\_many :contact\_addresses

and in contact\_address.rb  
belongs\_to :contact

Each contact may have more than one address in contact\_addresses table.  
Inorder to edit the address, I did the following:  
In the contact\_controller:

def edit  
@contact=Contact.find(params[:id])  
@contact\_address=ContactAddress.find :all, :conditions =\> [“contact\_id  
=?”, contact.id]  
end

edit.rhtml:

\<%for contact\_address in @contact\_address  
if i==1  
contact\_address1 = ContactAddress.new  
contact\_address1 = contact\_address  
else  
contact\_address2 = ContactAddress.new  
contact\_address2 = contact\_address  
end  
i=i+1  
end  
%\>

\<% puts contact\_address1.addr\_line1 %\> //This is working  
\<% puts contact\_address2.addr\_line1 %\> //This is working

\<%= text\_field "contact\_address1", "addr\_line1", :size =\> 50 %\> \<%= text\_field "contact\_address2", contact\_address2.addr\_line1", "size" =\> 50 %\>

But the above code is not working. The value is not printed in the text  
box. Can anyone please help me to find solution to this problem.

Regards…
