Can I export db data to fixtures?

Hi Guys,

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

(like the reverse of "rake db:fixtures:load")

Thanks, Arthur

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.

Brian Hogan 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 Chan

Thanks Brian, it seems to be what I want. :slight_smile:

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

Bobnation wrote:

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

On Dec 2, 1:00�am, Arthur Chan <rails-mailing-l...@andreas-s.net>

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.

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

<code> # 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

</code>