Rails and “legacy” DB’s

I’m importing a (so-called) “legacy” db, and was having some issues with creating associations because the primary key wasn’t named id. To fix this, simply use set_primary_key on the class, and then use foreign_key to create associations between classes as usual.

  1. class LegacyDatabaseTable < ActiveRecord::Base
  2.   set_primary_key :this_is_not_named_id
  3.   # …
  4. end