Code Prostitute

the sordid details of my career as a code prostitute

YourTable.reset_column_information

leave a comment »

Rails Migration: Make Database Changes Take Place Before Inserting / Updating Records

Let’s say you have a table to create, and start using it right away, in the same script. If you try to use it right away, it will error out as the table doesn’t exist yet.

class CreateJobLevels type)
end
end

def self.down
drop_table :job_levels
end
end

You need to use: reset_column_information

class CreateJobLevels < ActiveRecord::Migration
def self.up
create_table :job_levels do |t|
t.integer :id
t.string :name

t.timestamps
end

JobLevel.reset_column_information
%w{assistant executive manager director}.each do |type|
JobLevel.create(:name => type)
end
end

def self.down
drop_table :job_levels
end
end

Written by codeprostitute

November 17, 2009 at 2:58 pm

Posted in Uncategorized

Leave a Reply