@Brian,
I use the following script to create fixtures from a given sql
This is under my lib / task directory as rake task
namespace :fixture_generator do desc "generate fixtures for a given sql query from the current development database" task :fixture_generator, [:sql, :file_name] => :environment do |t, args| args.with_defaults(:sql => nil, :file_name => nil) i = "000" p "creating fixture - #{args.file_name}" File.open("#{Rails.root}/test/fixtures/#{args.file_name}.yml", 'a+') do |file| data = ActiveRecord::Base.connection.select_all(args.sql) file.write data.inject({}) { |hash, record| number = i.succ! hash["#{args.file_name}_#{number}"] = record hash }.to_yaml end end end
Using, say, I want to generate a binding for a user table
rake fixture_generator:fixture_generator["select * from users","users"]
And also, if you run another query with the same binding file name, it will add to the existing one
NTN
source share