# Newbie question: Trouble getting HABTM to return joined data

**URL:** <https://www.ruby-forum.com/t/newbie-question-trouble-getting-habtm-to-return-joined-data/49274>\
**Category:** Rails\
**Created:** [December 2, 2005, 12:12am UTC](https://www.ruby-forum.com/t/newbie-question-trouble-getting-habtm-to-return-joined-data/49274 "2005-12-02T00:12:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![esloane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@esloane](https://www.ruby-forum.com/u/esloane)\
**Post date:** [December 2, 2005, 12:12am UTC](https://www.ruby-forum.com/t/newbie-question-trouble-getting-habtm-to-return-joined-data/49274/1 "2005-12-02T00:12:50Z")

</div>

Hi,  
I’m having a stupid moment!  
two tables Jobs and Addresses are joined with jobs\_addresses.  
jobs\_addresses has Job\_id and Address\_id.  
In console I can query @job.addresses and see the attribute address\_id  
but I can’t figure out how to populate an address array (or hash) from  
that attribute. BTW, the purpose here is to return a list containing job  
data with address details attached  
Many Thanks,  
Eric

---

<div class="post-metadata">

**Author:** ![esloane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@esloane](https://www.ruby-forum.com/u/esloane)\
**Post date:** [December 2, 2005, 1:09am UTC](https://www.ruby-forum.com/t/newbie-question-trouble-getting-habtm-to-return-joined-data/49274/2 "2005-12-02T01:09:26Z")

</div>

Not quite clear what you want here, but maybe this will help you  
clarify.

First, has\_and\_belongs\_to\_many looks for the joined table names in  
alphabetical order. Yours are reversed, so make sure you map them:

has\_and\_belongs\_to\_many :addresses, :join\_table =\> “jobs\_addresses”

Second, remember you are dealing with a collection, so to deal with  
each item you can call each and pass a block, or use find() to get  
the specific item:

job.addresses.each do |address|  
#do something to address  
end

address = job.addesses.find()  
#do something to the address

Jamie

---

<div class="post-metadata">

**Author:** ![esloane](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e19b73/32.png) [@esloane](https://www.ruby-forum.com/u/esloane)\
**Post date:** [December 2, 2005, 2:06am UTC](https://www.ruby-forum.com/t/newbie-question-trouble-getting-habtm-to-return-joined-data/49274/3 "2005-12-02T02:06:02Z")

</div>

Jamie Orchard-Hays wrote:

> > * * *
> > 
> > Rails mailing list  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

Hi Jamie - thanks for the response.

What I want to get is a list of jobs containing job\_number, job\_type,  
etc. from Jobs. Appended to each of those I want address\_1, address\_2,  
suburb, etc. from Addresses.

I have the models set to specify the join\_table so that’s OK (I found  
the lexical rating thing yesterday)

When I query @job.addresses in console I see an address\_id equal to 2  
which is correct. When I try to get that to return an actual address I  
run into problems - NoMethodError, etc… So, I guess I’m asking How do I  
get to the address table with this information so eadch job will display  
an address in the list.

Thanks again for helping - I guess this is a fundamentally dumb  
question, but I’m new to both Ruby and Rails and need some help in  
understanding the relationships  
Cheers,  
Eric.
