# Override the name of a Join Table

**URL:** <https://www.ruby-forum.com/t/override-the-name-of-a-join-table/50083>\
**Category:** Rails\
**Created:** [December 12, 2005, 9:40pm UTC](https://www.ruby-forum.com/t/override-the-name-of-a-join-table/50083 "2005-12-12T21:40:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gael\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc8723/32.png) [@Gael\_P](https://www.ruby-forum.com/u/Gael_P)\
**Post date:** [December 12, 2005, 9:40pm UTC](https://www.ruby-forum.com/t/override-the-name-of-a-join-table/50083/1 "2005-12-12T21:40:40Z")

</div>

Hi all, is there any way we can override the name of a join table?  
I’ve got 2 tables called “Location” and “Index” and the join table to  
link  
the two together is called “LocationIndex” instead of “location\_index”  
like  
Rails expects it.  
I’ve even created a model for this join table and I can access its  
content  
fine but everytime it tries to use the join table, Rails comes up with  
the  
following error:

Invalid object name ‘location\_index’, indeed the table is called  
“LocationIndex”

PS: I’m working on a legacy database so I cannot change the name of the  
tables.  
PSS: Even if it means hacking the source I’m willing to look at it. I’ve  
tried myself to find out where in ActiveRecord this was taking place but  
I  
couldn’t find anything interesting.

Gael

---

<div class="post-metadata">

**Author:** ![Gael\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc8723/32.png) [@Gael\_P](https://www.ruby-forum.com/u/Gael_P)\
**Post date:** [December 12, 2005, 10:01pm UTC](https://www.ruby-forum.com/t/override-the-name-of-a-join-table/50083/2 "2005-12-12T22:01:52Z")

</div>

Hi Gael,

try to add :join\_table in your model, like:

has\_and\_belongs\_to\_many :locations, :join\_table =\> ‘LocationIndex’

Nick

Gael P. wrote:

> Hi all, is there any way we can override the name of a join table?  
> I’ve got 2 tables called “Location” and “Index” and the join table to  
> link  
> the two together is called “LocationIndex” instead of “location\_index”  
> like  
> Rails expects it.  
> I’ve even created a model for this join table and I can access its  
> content  
> fine but everytime it tries to use the join table, Rails comes up with  
> the  
> following error:
> 
> Invalid object name ‘location\_index’, indeed the table is called  
> “LocationIndex”
> 
> PS: I’m working on a legacy database so I cannot change the name of the  
> tables.  
> PSS: Even if it means hacking the source I’m willing to look at it. I’ve  
> tried myself to find out where in ActiveRecord this was taking place but  
> I  
> couldn’t find anything interesting.
> 
> Gael

---

<div class="post-metadata">

**Author:** ![Gael\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc8723/32.png) [@Gael\_P](https://www.ruby-forum.com/u/Gael_P)\
**Post date:** [December 12, 2005, 10:13pm UTC](https://www.ruby-forum.com/t/override-the-name-of-a-join-table/50083/3 "2005-12-12T22:13:44Z")

</div>

Hey Gael,

I just learned this today! You want to take a look at  
[http://api.rubyonrails.com](http://api.rubyonrails.com) - wealth of documentation. But in your model  
you  
want to add a line something like the following:

In your Location model:  
has\_and\_belongs\_to\_many :indexes, :join\_table =\> “LocationIndex”

and in your Index model  
has\_and\_belongs\_to\_many :locations, :join\_table =\> “LocationIndex”

HTH,

Mel

---

<div class="post-metadata">

**Author:** ![Gael\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc8723/32.png) [@Gael\_P](https://www.ruby-forum.com/u/Gael_P)\
**Post date:** [December 13, 2005, 12:59am UTC](https://www.ruby-forum.com/t/override-the-name-of-a-join-table/50083/4 "2005-12-13T00:59:30Z")

</div>

should the join table not be called IndexLocation rather than  
LocationIndex? I thought rails gave trouble when the join table name  
is not built in alphabetical order.

bruce

---

<div class="post-metadata">

**Author:** ![Gael\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/bc8723/32.png) [@Gael\_P](https://www.ruby-forum.com/u/Gael_P)\
**Post date:** [December 17, 2005, 1:41pm UTC](https://www.ruby-forum.com/t/override-the-name-of-a-join-table/50083/5 "2005-12-17T13:41:51Z")

</div>

Thanks Mel, I’ve just found it too…I guess I was trigger happy posting  
on  
that one and I should have RTFM a bit more before. I was too busy diving  
in  
the source instead of just looking at Rdoc.

This is great…Rail reminds me QT, whenever I wanted a feature it was  
there  
ready to be used…

Gael
