Fixtures question

Quick question on fixtures. I know fixtures are primarily for unit
testing but I was thinking of using them to help me with development. I
need some realistic data so with the ERb templated fixtures it seems
like I could generate that and insert it in my development database.
That would give me real data to work with (instead of just a few records
I have manually inserted in each table). But I have two questions on
this:

  1. Is there an easy way that I can say “load these fixtures into this
    database” or do I need to look at the Fixtures API and write a script do
    this (just wondering if there is already a rake task or something that
    will do this).

  2. When it loads the fixture does it create a instance of that object
    then call save or do a straight insert. The reason I was wondering is
    because some fields are generated by ActiveRecord (updated_on,
    nested_set left and right columns, etc) and it seems that this data
    wouldn’t be generated. I would prefer not to have to type this stuff
    into the YAML file myself.

Would it be better just to write a console script that uses ActiveRecord
to create my test data?

Eric

eric wrote:

Quick question on fixtures. I know fixtures are primarily for unit
testing but I was thinking of using them to help me with development. I
need some realistic data so with the ERb templated fixtures it seems
like I could generate that and insert it in my development database.
That would give me real data to work with (instead of just a few records
I have manually inserted in each table). But I have two questions on
this:

  1. Is there an easy way that I can say “load these fixtures into this
    database” or do I need to look at the Fixtures API and write a script do
    this (just wondering if there is already a rake task or something that
    will do this).

You can load your test fixtures in your development database.

rake load_fixtures

If you have constraints you need to add to the file
ruby\lib\ruby\gems\1.8\gems\rails-1.0.0\lib\tasks\databases.rake:

desc “Loads fixtures whilst turning foreign key constraints checking
off”
task :load_fixtures_without_constraints => :environment do
require ‘active_record/fixtures’
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
ActiveRecord::Base.connection.update “SET FOREIGN_KEY_CHECKS = 0”
Dir.glob(File.join(RAILS_ROOT, ‘test’, ‘fixtures’,
.{yml,csv}’)).each do |fixture_file|
Fixtures.create_fixtures(‘test/fixtures’,
File.basename(fixture_file, '.
’))
end
ActiveRecord::Base.connection.update “SET FOREIGN_KEY_CHECKS = 1”
end

I found this somewhere in the internet.

Now you can load your fixtures mit rake
load_fixtures_without_constraints.

eric wrote:

Quick question on fixtures. I know fixtures are primarily for unit
testing but I was thinking of using them to help me with development. I
need some realistic data so with the ERb templated fixtures it seems
like I could generate that and insert it in my development database.

FYI, Chad F.s “Rails Recipes” has a few good bits and pieces on
working with fixtures. Beta available now from Prags.

Alan

At my work, we wanted to have different fixtures for development than
for
testing- this is how we did it.

First, we created a directory called db/fixtures, and we added all the
relevant fixture files.

Next, we added a Rake task file called fixtures.rake, in the lib\tasks
directory. That file contains (among other things):

desc “Load fixtures data into the development database”
task :load_development_fixtures => :environment do
load_fixtures_helper :development, ‘db/fixtures’
end

def load_fixtures_helper(env, path)
require ‘active_record/fixtures’
ActiveRecord::Base.establish_connection(env)
Fixtures.create_fixtures(path,
ActiveRecord::Base.configurations[:fixtures_load_order])
end

Then, to control the load order, I added this to config/environment.rb:

ActiveRecord::Base.configurations[:fixtures_load_order] = [
:fixture_1,
:fixture_2,
:fixture_3
]

Now, I can call “rake load_development_fixtures” from the command line,
and
my development fixtures will be loaded into the development DB. Using a
helper function let me create other tasks, such as load demo fixtures,
or
load minimal fixtures.