HABTM join table has an "ID" column - is this an issue?

All,

I’m building model objects for existing tables that I cannot modify.

In AWDWR, Dave says “Note that our join table has no id column…The
second reason for not including an id column in the join table is that
AR automatically includes all columns from the join tables when
accessing rows using it. If the join table included a column called id,
its id would overwrite the id of the rows in the joined tables.”

Unfortunately, my join table does have a column called id. Is there a
directive that can help me with this or some way to get around it per
Dave T.'s quote above?

Thanks,
Wes

Wes G. wrote:

All,

I’m building model objects for existing tables that I cannot modify.

In AWDWR, Dave says “Note that our join table has no id column…The
second reason for not including an id column in the join table is that
AR automatically includes all columns from the join tables when
accessing rows using it. If the join table included a column called id,
its id would overwrite the id of the rows in the joined tables.”

Unfortunately, my join table does have a column called id. Is there a
directive that can help me with this or some way to get around it per
Dave T.'s quote above?

Thanks,
Wes

Should I provide a value for the :select option, which will keep a
SELECT * from happening on my join table?

You could upgrade the join table to a full model and use has_many
:through in place of habtm.

Tom W. wrote:

You could upgrade the join table to a full model and use has_many
:through in place of habtm.

I just realized, however, that although my join table has an “id”
column, neither of the join_ed_ tables on either side of the association
have “id” columns.

So, I’m thinking that there shouldn’t be any collision between columns
here.

Where can I find out more about has_many :through? - I’m unfamiliar with
this option.

On 7/5/06, Wes G. [email protected] wrote:

Where can I find out more about has_many :through? - I’m unfamiliar with
this option.

http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000530

http://wiki.rubyonrails.org/rails/pages/ThroughAssociations

Tom

Wes G. wrote:

Tom W. wrote:

You could upgrade the join table to a full model and use has_many
:through in place of habtm.

I just realized, however, that although my join table has an “id”
column, neither of the join_ed_ tables on either side of the association
have “id” columns.

So, I’m thinking that there shouldn’t be any collision between columns
here.

Where can I find out more about has_many :through? - I’m unfamiliar with
this option.

You shouldn’t have an “id” column in a join table - that will just get
you in trouble. If you have anything in your join table besides the
foreign_keys of the two tables being joined, you’ll have problems. In
that case, you’ll want to use a join model (has_many :through) instead.

http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off


Josh S.
http://blog.hasmanythrough.com

Josh S. wrote:

Wes G. wrote:

Tom W. wrote:

You could upgrade the join table to a full model and use has_many
:through in place of habtm.

I just realized, however, that although my join table has an “id”
column, neither of the join_ed_ tables on either side of the association
have “id” columns.

So, I’m thinking that there shouldn’t be any collision between columns
here.

Where can I find out more about has_many :through? - I’m unfamiliar with
this option.

You shouldn’t have an “id” column in a join table - that will just get
you in trouble. If you have anything in your join table besides the
foreign_keys of the two tables being joined, you’ll have problems. In
that case, you’ll want to use a join model (has_many :through) instead.

http://blog.hasmanythrough.com/articles/2006/04/20/many-to-many-dance-off


Josh S.
http://blog.hasmanythrough.com

Will I have problems if neither of the two tables in the HABTM
relationship has an “id” column?

WG

Wes G. wrote:

Will I have problems if neither of the two tables in the HABTM
relationship has an “id” column?

ActiveRecord requires a primary key to manage object identity and to do
joins between tables. If you have a primary key but have renamed the
column from “id” to something else, you’ll be fine. If not, you’ll need
to change something, or habtm won’t have anything to work with to do the
joins.


Josh S.
http://blog.hasmanythrough.com