Forum: Ruby on Rails Override the name of a Join Table

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gael P. (Guest)
on 2005-12-12 22:40
(Received via mailing list)
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
Nick S. (Guest)
on 2005-12-12 23:01
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
Mel R. (Guest)
on 2005-12-12 23:13
(Received via mailing list)
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
Bruce B. (Guest)
on 2005-12-13 01:59
(Received via mailing list)
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
Gael P. (Guest)
on 2005-12-17 14:41
(Received via mailing list)
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
This topic is locked and can not be replied to.