Override the name of a Join Table

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

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

Hey Gael,

I just learned this today! You want to take a look at
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

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

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