Rails ActiveRecord has and belongs to many

December 28th, 2010

I'm back working on a Rails application and since its Rails 2.x, I'm using ActiveRecord. That is of course fine, but most of my experience has been with DataMapper, which plays nicely with Merb and Rails 3.

Yesterday I was working on some models and their associations, starting with the generation of the models:

script/generate model Company name:string
script/generate model Industry name:string

Then I got to the step where I indicated in the model that each object could be associated with any number of its associated objects and so of course I used something like this:

class Company
  has_and_belongs_to_many :industries
class Industry
  has_and_belongs_to_many :companies

That is good, but what about the database migrations for a many-to-many ActiveRecord association for Rails? Turns out that since the existing migrations were automatically created prior to the associations, at least one needed to be edited prior to being run via rake. I added something like this to the migration that was automatically generated when I created the industry model:

create_table :companies_industries, :id => false do |t|
  t.references :company, :industry

After that, I ran:

rake db:migrate

It worked like a charm!

Yearly Indexes: 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2015 2019 2020 2022