# Displaying hash for has\_many

**URL:** <https://www.ruby-forum.com/t/displaying-hash-for-has-many/143591>\
**Category:** Rails\
**Created:** [July 21, 2008, 12:01am UTC](https://www.ruby-forum.com/t/displaying-hash-for-has-many/143591 "2008-07-21T00:01:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeff.Corcoran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@Jeff.Corcoran](https://www.ruby-forum.com/u/Jeff.Corcoran)\
**Post date:** [July 21, 2008, 12:01am UTC](https://www.ruby-forum.com/t/displaying-hash-for-has-many/143591/1 "2008-07-21T00:01:51Z")

</div>

Let me start off by saying I am very much a beginner.

I currently have 2 models - Person, and address.

I am currently trying to display a person and their address on the  
“index” page in the person section.

I can create a person and multiple addresses all at once using the  
Advanced Rails Recipe, and the foreign key saves fine, but now I am  
trying to get the person to display with their associated address on  
the index page and am lost.

\<% if @people -%\>

\<% end -%\>

The above code spits out the proper string for all the people I want,  
however, I can’t seem to figure out how to spit out their addresses as  
well.

def index  
@people = Person.find(:all)  
respond\_to do |format|  
format.html # index.html.erb  
format.xml { render :xml =\> @people }  
end  
end

The above is what I have in my person controller currently, and I have  
no idea where to go from here to get this done. Any help would be  
greatly appreciated.

---

<div class="post-metadata">

**Author:** ![Jeff.Corcoran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@Jeff.Corcoran](https://www.ruby-forum.com/u/Jeff.Corcoran)\
**Post date:** [July 21, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/displaying-hash-for-has-many/143591/2 "2008-07-21T15:43:52Z")

</div>

Assuming you have set up relationships correctly either has\_one or  
has\_many between Person and Address. It should be as simple as  
@person.address (or @person.address1 etc.). The @person instance  
variable will carry the associated data with it so you simply have to  
use the “.” operator to get the address data.  
Bharat

---

<div class="post-metadata">

**Author:** ![Jeff.Corcoran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@Jeff.Corcoran](https://www.ruby-forum.com/u/Jeff.Corcoran)\
**Post date:** [July 21, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/displaying-hash-for-has-many/143591/3 "2008-07-21T17:16:37Z")

</div>

.to\_json takes an :include param–have a look at the activerecord docs.
