# Can I export db data to fixtures?

**URL:** <https://www.ruby-forum.com/t/can-i-export-db-data-to-fixtures/154604>\
**Category:** Rails\
**Created:** [December 2, 2008, 3:14am UTC](https://www.ruby-forum.com/t/can-i-export-db-data-to-fixtures/154604 "2008-12-02T03:14:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 2, 2008, 3:14am UTC](https://www.ruby-forum.com/t/can-i-export-db-data-to-fixtures/154604/1 "2008-12-02T03:14:58Z")

</div>

Hi Guys,

Is there any way to export db data to fixtures files?

(like the reverse of “rake db:fixtures:load”)

Thanks,  
Arthur

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 2, 2008, 4:12am UTC](https://www.ruby-forum.com/t/can-i-export-db-data-to-fixtures/154604/2 "2008-12-02T04:12:56Z")

</div>

Yeah there are a few plugins to do that. There’s one called  
ar\_fixtures that should work just fine. I don’t know if it’s  
supported any more but I know it still works fine.

On Mon, Dec 1, 2008 at 8:14 PM, Arthur C.

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 2, 2008, 8:00am UTC](https://www.ruby-forum.com/t/can-i-export-db-data-to-fixtures/154604/3 "2008-12-02T08:00:22Z")

</div>

Brian H. wrote:

> Yeah there are a few plugins to do that. There’s one called  
> ar\_fixtures that should work just fine. I don’t know if it’s  
> supported any more but I know it still works fine.
> 
> On Mon, Dec 1, 2008 at 8:14 PM, Arthur C.

Thanks Brian, it seems to be what I want. 🙂

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 2, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/can-i-export-db-data-to-fixtures/154604/4 "2008-12-02T15:19:34Z")

</div>

I’m curious as to what you are using this for.

On Dec 2, 1:00 am, Arthur C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 3, 2008, 10:08am UTC](https://www.ruby-forum.com/t/can-i-export-db-data-to-fixtures/154604/5 "2008-12-03T10:08:57Z")

</div>

Bobnation wrote:

> I’m curious as to what you are using this for.
> 
> On Dec 2, 1:00ï¿½am, Arthur C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

It is difficult to handle the data with relationships between different  
tables. So, it is better to start the creation of fixtures from using  
existing data.

Also, it is useful for us to generate demo site.

---

<div class="post-metadata">

**Author:** ![artchrst](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e36b37/32.png) [@artchrst](https://www.ruby-forum.com/u/artchrst)\
**Post date:** [December 3, 2008, 10:10am UTC](https://www.ruby-forum.com/t/can-i-export-db-data-to-fixtures/154604/6 "2008-12-03T10:10:18Z")

</div>

I just modified the plugin (the ar\_fixtures.rake file) to allow output  
all tables:

`
# arthur: if "rake db:data:dump MODEL=ALL" then load the following 
models
# put all your tables need to export here
all_modelsS = ['Album', 'Asset',...
              'UserContact', 'UserInfo', 'User']
def env_or_raise(var_name, human_name)
if ENV[var_name].blank?
raise “No #{var_name} value given. Set #{var_name}=#{human_name}”
arthur: allow the ALL input
elsif var_name == ‘MODEL’ && ENV[var_name] == ‘ALL’
return all_models
else
return ENV[var_name]
end
#else
return ENV[var_name]
#end
end
arthur: check if all model is needed
def all_models
return all_modelsS if ENV[‘MODEL’] == ‘ALL’
input = env_or_raise(‘MODEL’, ‘ModelName’)
return [input]
end
def model_or_raise
return env_or_raise(‘MODEL’, ‘ModelName’)
end
def limit_or_nil_string
ENV[‘LIMIT’].blank? ? ‘nil’ : ENV[‘LIMIT’]
end
namespace :db do
namespace :fixtures do
desc “Dump data to the test/fixtures/ directory. Use MODEL=ModelName
and LIMIT (optional)”
task :dump => :environment do
# arthur, allow all models input
all_models.each do | model|
eval “#{model}.to_fixture(#{limit_or_nil_string})”
end
# eval “#{model_or_raise}.to_fixture(#{limit_or_nil_string})”
end
end
namespace :data do
desc “Dump data to the db/ directory. Use MODEL=ModelName and LIMIT
(optional)”
task :dump => :environment do
# arthur, allow all models input
all_models.each do | model|
eval “#{model}.dump_to_file(nil, #{limit_or_nil_string})”
puts “#{model} has been dumped to the db folder.”
end
end
```
desc "Load data from the db/ directory. Use MODEL=ModelName"
task :load => :environment do
  # arthur, allow all models input
   all_models.each do | model|
    eval "#{model}.load_from_file"
  end
  #eval "#{model_or_raise}.load_from_file"
end

```
end
end
`
