Converting join table to join model table

I have a join table (foos_bars) which used to just be a join table in a
HABTM
relationship.

Now I want to create a FooBars model to hold extra attributes about the
relationship without losing any of the data in the table, however I
can’t think
of a way to add the promary key column without an error. How do I do it?

Thanks

doesn’t rails add an id column anyway?

A HABTM association doesn’t use an id primary key in the table
definition. The structure is similar to:

table ‘table1_table2’
t.column table1_id, :integer
t.column table2_id, :integer

Since you’re now stating you want the join to be pushed to a separate
model now, with attributes; you’ll change both model definitions to
have has_many :myNewJoinyThing. The model will also have the same
association. Since it is a separate model you can add attributes as
well.

Gareth A. wrote:

I have a join table (foos_bars) which used to just be a join table in a
HABTM relationship.

Now I want to create a FooBars model to hold extra attributes about the
relationship without losing any of the data in the table, however I
can’t think
of a way to add the promary key column without an error. How do I do it?

Turning a join table into a full-fledged join model requires a few
changes:

  1. Rename the table from foos_bars to fubars, or whatever is compatible
    with your model name.
  2. Add an ‘id’ field for the primary key, auto-increment int
  3. Fix all the associations that used habtm to use has_many :through.

I’ve heard reports that doing step 2 using migrations can cause
problems. It might be better to create the new join model table and
transfer the data from the old table to the new one.


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