# Fixtures question

**URL:** <https://www.ruby-forum.com/t/fixtures-question/47508>\
**Category:** Rails\
**Created:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/fixtures-question/47508 "2005-11-12T12:49:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [November 12, 2005, 12:49pm UTC](https://www.ruby-forum.com/t/fixtures-question/47508/1 "2005-11-12T12:49:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [April 3, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/fixtures-question/47508/2 "2006-04-03T17:24:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [April 3, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/fixtures-question/47508/3 "2006-04-03T17:40:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eric](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/898d66/32.png) [@eric](https://www.ruby-forum.com/u/eric)\
**Post date:** [April 3, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/fixtures-question/47508/4 "2006-04-03T18:17:28Z")

</div>

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.
