Forum: Ruby on Rails Fixtures question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
5190330ad8f1b06d35e2c2da73dc623c?d=identicon&s=25 eric (Guest)
on 2005-11-12 12:49
(Received via mailing list)
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
62c1d85cd91fafdddbb545e7e771467e?d=identicon&s=25 Marc Eckart (meckart)
on 2006-04-03 17:24
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.
4daf0b71d5d9a3882e583c0e72eaf5dc?d=identicon&s=25 Alan Francis (Guest)
on 2006-04-03 17:40
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 Fowlers "Rails Recipes" has a few good bits and pieces on
working with fixtures.  Beta available now from Prags.

Alan
3ec705c5dd3480c6268b72c5617e8dae?d=identicon&s=25 Michael Smedberg (Guest)
on 2006-04-03 18:17
(Received via mailing list)
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.
This topic is locked and can not be replied to.