Adding STI to Existing Table...

Posted by keruilin on Stack Overflow See other posts from Stack Overflow or by keruilin
Published on 2010-04-11T12:15:50Z Indexed on 2010/04/11 12:23 UTC
Read the original article Hit count: 321

I want to add STI to an existing table using a custom type column. Let's call this taste_type whose corresponding model is Fruit.

In the Fruit model I have:

set_inheritance_column :taste_type

In my migration to add STI I have:

class AddSTI < ActiveRecord::Migration
  def self.up
    add_column :fruits, :taste_type, :string, :limit => 100, :null => false
    Fruit.reset_column_information
    Fruit.find_by_id(1).update_attributes({:taste_type => 'Sour'})
  end

  def self.down
    remove_column :fruits, :taste_type
  end

end

When I run the migration, I get the following error:

Mysql::Error: Column 'taste_type' cannot be null: ...

Any idea what's going? I can get the migration to run if I comment the set_inheritance_column in the Fruit model, then uncomment it after I run the migration. Obviously, I don't want to do this, however.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about single-table-inheritance