# Newbie: Going round in circles with HABTM

**URL:** <https://www.ruby-forum.com/t/newbie-going-round-in-circles-with-habtm/51832>\
**Category:** Rails\
**Created:** [December 3, 2005, 6:22am UTC](https://www.ruby-forum.com/t/newbie-going-round-in-circles-with-habtm/51832 "2005-12-03T06:22:48Z")\
**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 3, 2005, 6:22am UTC](https://www.ruby-forum.com/t/newbie-going-round-in-circles-with-habtm/51832/1 "2005-12-03T06:22:48Z")

</div>

Hi,  
I’m still stuck with a fundamental problem understanding how this hangs  
together. Given two tables with a HABTM relationship through a  
join\_table as follows;

Model job:  
class Job \< ActiveRecord::Base  
belongs\_to :JobTypes  
has\_many :People  
has\_and\_belongs\_to\_many :addresses, :join\_table =\> “Jobs\_Addresses”  
has\_many :Reminders  
has\_many :JobPhases  
has\_many :Discussions  
has\_many :actions  
has\_one :jobtype  
has\_many :documents  
has\_one :organisation  
has\_many :reminders  
has\_many :discussions  
validates\_associated :address  
end

Model address:  
class Address \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :jobs, :join\_table =\> “Jobs\_Addresses”  
has\_one :Address\_type  
has\_one :organisation  
end

Model job\_address  
class JobAddress \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :jobs  
has\_and\_belongs\_to\_many :addresses  
end

OK, heres my problem =\>  
In console I get to a job.  
I can read its data, methods and attributes and I can return the address  
data as follows;

> > @job.addresses  
> > =\> [#\<Address:0x37dd170 @attributes={“Organisation\_ID”=\>nil,  
> > “Address\_2”=\>nil, “Job\_ID”=\>“4”, “Country”=\>“Australia”, “id”=\>“2”,  
> > “Suburb”=\>“Mitcham”, “OrgType\_ID”=\>nil, “State”=\>“Vic”,  
> > “Address\_1”=\>“456, second st”, “Address\_ID”=\>“2”}\>]

What I can’t figure is how to get at that attribute data to put into a  
simple job list mixing the address in with the other job attributes!

Can somebody please explain before I go nuts!!

Kind Regards,  
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 3, 2005, 6:59am UTC](https://www.ruby-forum.com/t/newbie-going-round-in-circles-with-habtm/51832/2 "2005-12-03T06:59:39Z")

</div>

On 12/2/05, Eric S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> has\_and\_belongs\_to\_many :addresses, :join\_table =\> “Jobs\_Addresses”

If you rename your join table alphabetically (ie addresses\_jobs) then  
you  
won’t have to specify :join\_table because the Rails convention will find  
it  
automatically.

> > @job.addresses  
> > =\> [#\<Address:0x37dd170 @attributes={“Organisation\_ID”=\>nil,  
> > “Address\_2”=\>nil, “Job\_ID”=\>“4”, “Country”=\>“Australia”, “id”=\>“2”,  
> > “Suburb”=\>“Mitcham”, “OrgType\_ID”=\>nil, “State”=\>“Vic”,  
> > “Address\_1”=\>“456, second st”, “Address\_ID”=\>“2”}\>]
> 
> What I can’t figure is how to get at that attribute data to put into a  
> simple job list mixing the address in with the other job attributes!

Since a job may have more than one address, maybe you want to do this

\<% @job.addresses.each do |address| %\>  
\<%= address.Suburb %\>, \<%= address.Country %\>  
\<% end %\>

Also, I think that Rails people do not usually capitalize db table and  
field  
names. If you are using a legacy db then this comment and the one about  
the  
join table name are probably just annoying:)

Hope that helps.

Peter

---

<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 3, 2005, 7:27am UTC](https://www.ruby-forum.com/t/newbie-going-round-in-circles-with-habtm/51832/3 "2005-12-03T07:27:58Z")

</div>

Peter M. wrote:

> you won’t have to specify :join\_table because the Rails convention will
> 
> Also, I think that Rails people do not usually capitalize db table and
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)  
> Thanks Peter,  
> I knew it was something simple! The only annoying thing is the headache  
> I have from trying to figure this out!!  
> Thanks Again,  
> Eric.
