“Loading data in migrations”

July 8, 2008

I found migrations to be very useful tool to maintain data structures in a rails project. It is clean, easy to understand and fast when it comes to recreate a database.

During develoment, I faced several times the same problem: “How to import static data into the database?” Of course, one can create fixture and load it. But sometimes one needs more powerful tool.

So, I tried to import the data in the migrations themselves. Since the data to import was huge (tens of MBs), I did try several methods and approaches. Here is the result.

Fixtures in Migrations

This is method that I found on the web in an article of Adam Christensen Loading Fixtures in a Migration.
It shows how to load the fixtures file into database.

require 'active_record/fixtures'<br /><br />class CreateCategories < ActiveRecord::Migration<br />  def self.up<br />    create_table :categories do |t|<br />      t.column :name, :string<br />    end<br /><br />    Fixtures.create_fixtures(’test/fixtures’, File.basename(”categories.yml”, ‘.*’))<br />  end<br />  def self.down<br />    drop_table :categories<br />  end<br />end<br /><br /><br />

Entry Filed under: loading data in migrations, rails, ruby on rails. Tags: , , .

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Recent Posts

Archives

Category Cloud

Active Record active scaffold ajax calendar date select capistrano deployment edge rails formula 1 2008 For those who dont want to update the prototype.js Framesets vs iFrames fun html javascript mysql plugin rails regexp ror Ruby ruby on rails ruy on rails SQL string functions Text Area they can checkout 1.8.3 of the calender date select titleize titleize vs capitalize tutorial Uncategorized validations

Tags

Links

Meta

Pages

Calendar

July 2008
M T W T F S S
« Jun   Aug »
 123456
78910111213
14151617181920
21222324252627
28293031