add_column for references (Rails) add_column for references (Rails) ruby-on-rails ruby-on-rails

add_column for references (Rails)


While it's too late to get any points out of this, I thought I'd post the best way for posterity :)

use change_table instead of create_table to add columns to a table that already exists, with all the TableDefinition goodness:

self.up do  change_table :comments do |t|    t.references :author  endend

This might seem trivial, but other gems like Devise make heavy use of their own custom table definitions, and this way you can still use them.


add_reference :table_name, :reference, index: true


Finally got it

add_column :locations, :state_id , :integer, :references => "states"