Here’s one way of building initial data into a migration. I put the data into a subdirectory of db/migrate called ‘initial_data’ so it’s a bit more clear that it was relevant at a particular point in time. You could certainly get the data some other way that using YAML fixtures loaded by ActiveRecord, but that all depends on what format your data has now.
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
=== db/migrate/001_create_borogroves.rb
require ‘active_record/fixtures’
class CreateBorogroves < ActiveRecord::Migration
class Borogrove < ActiveRecord::Base
belongs_to :jabberwocky
acts_as_list :scope => :jabberwocky
serialize :toves
end
def self.up
create_table :borogroves do |t|
t.column :name, :string
t.column :description, :string
t.column :jabberwocky_id, :integer
t.column :position, :integer
t.column :toves, :text
end
Borogrove.reset_column_information
say_with_time(“Load initial data from fixture”) do
directory = File.join(File.dirname(FILE), “initial_data”)
say “Fixture directory is: #{directory}”, true
Fixtures.create_fixtures(directory, Borogrove.table_name)
end
end
def self.down
drop_table :borogroves
end
end
=== db/migrate/initial_data/borogroves.yml
<% stanza = Jabberwocky.find_by_name(‘stanza’) %>
<% counter = 0 %>
stanza_<%= counter += 1 %>:
id: <%= counter %>
name: “Fred”
description: “short”
jabberwocky_id: <%= stanza.id %>
position: <%= counter %>
troves:
gyre: “only when brillig”
gimble: occasionally
slithy: certainly
and so on…