# Join tables

**URL:** <https://www.ruby-forum.com/t/join-tables/185918>\
**Category:** Rails\
**Created:** [March 29, 2010, 9:34am UTC](https://www.ruby-forum.com/t/join-tables/185918 "2010-03-29T09:34:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [March 29, 2010, 9:34am UTC](https://www.ruby-forum.com/t/join-tables/185918/1 "2010-03-29T09:34:45Z")

</div>

Just working out my db schema before I start development and I got  
stuck on my join tables. With the way most associations work in rails  
already. Are join tables really needed? Rails will handle most of that  
auto magically so won’t building and maintaining these tables in the  
application by hand be redundant?

Just wondering your opinions.

Cheers,

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [March 29, 2010, 10:09am UTC](https://www.ruby-forum.com/t/join-tables/185918/2 "2010-03-29T10:09:53Z")

</div>

On 29 March 2010 08:33, brianp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Just working out my db schema before I start development and I got  
> stuck on my join tables. With the way most associations work in rails  
> already. Are join tables really needed? Rails will handle most of that  
> auto magically so won’t building and maintaining these tables in the  
> application by hand be redundant?

Don’t start by working out the db schema, start by defining the models  
that map the objects you are modeling in the real world, then define  
the associations between them. Whether you need hand crafted join  
tables or not will then become clear.

Colin

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [March 29, 2010, 10:35am UTC](https://www.ruby-forum.com/t/join-tables/185918/3 "2010-03-29T10:35:38Z")

</div>

Sorry I deleted my original post as I thought I had figured it out  
well talking out loud.

Well Model defining and the db schema kinda go hand in hand. I guess  
I’m really just defining the model, but doing it in mysql workbench  
=) . I’m bad with pen & paper =P

I’d have to say though at this point I’m still having trouble deciding  
if I would need my own join tables. For most of my tables I’m thinking  
not. The more I think about I can’t think of any situation I would  
need a join table anymore but then I wonder if it’s just ignorance  
lol. Support tickets for example. A Ticket will have many updates.  
I’ll always find the ticket\_updates via :through & :has\_many. And if I  
have a ticket\_update I can always find the ticket because ticket\_id  
would be stored in the ticket\_update. This is just a single example of  
which I may or may not have done “correctly.” But I really just can’t  
think of an instance that this system wouldn’t work. Every db I’ve  
made all relationships have worked out like that. I mean I could brake  
that up into a join table but it seems like it would take more effort  
to query the join table and get the results then to just do it like  
mentioned above.

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [March 29, 2010, 10:53am UTC](https://www.ruby-forum.com/t/join-tables/185918/4 "2010-03-29T10:53:47Z")

</div>

On 29 March 2010 09:32, brianp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> need a join table anymore but then I wonder if it’s just ignorance  
> lol. Support tickets for example. A Ticket will have many updates.  
> I’ll always find the ticket\_updates via :through & :has\_many. And if I  
> have a ticket\_update I can always find the ticket because ticket\_id  
> would be stored in the ticket\_update.

That is what I mean by defining the models and associations first. If  
you have  
ticket has\_many updates  
and  
update belongs to ticket  
then why would you need a ticket\_updates table at all? If you have a  
ticket then the updates are available via my\_ticket.updates and if you  
have an update then it’s ticket is the\_update.ticket.

Colin

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [March 29, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/join-tables/185918/5 "2010-03-29T19:28:56Z")

</div>

I see what your saying. Maybe it was just the late night but I was  
having a problem getting my head around it. Really the best time for a  
join table is in a habtm relationship. Then it would be a good idea.  
Like If something was taggable. Would be a good example of a need for  
a join table.

Thanks for your replies. I hope it was just a momentary lapse on my  
behalf cause I know I knew that before lol.

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [March 29, 2010, 10:50am UTC](https://www.ruby-forum.com/t/join-tables/185918/6 "2010-03-29T10:50:51Z")

</div>

> I’d have to say though at this point I’m still having trouble deciding  
> if I would need my own join tables. For most of my tables I’m thinking  
> not. The more I think about I can’t think of any situation I would  
> need a join table anymore but then I wonder if it’s just ignorance  
> lol. Support tickets for example. A Ticket will have many updates.  
> I’ll always find the ticket\_updates via :through & :has\_many. And if I  
> have a ticket\_update I can always find the ticket because ticket\_id  
> would be stored in the ticket\_update.

Imagine that your Support tickets system has ability to attach some  
tags to ticket. How would you implement that?  
Generally join tables are used when there is has\_and\_belongs\_to\_many  
association.  
One can look at has\_many :through as a join table fat enough to become  
a model itself.

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)
