If you've got a boat load of legacy fixtures laying around (and who doesn't?) it can be a pain now that Rails handles the ID of objects so you don't have to.
On the train back from holiday I wrote a snippet of code to migrate them:
RUBY:
-
def Util.real_yml(klass,col = 'name')
-
y = YAML.load_file(File.join(RAILS_ROOT,'test','fixtures',"#{klass.to_s.tableize}.yml"))
-
real_names = y.inject([]) { |names,(key,val)| names <<y[key][col] if y[key][col] ;names }
-
real_objs = klass.find :all, :conditions => [ "#{col} IN (?)",real_names ],
rder => "#{col} asc" -
yml = ""
-
real_objs.each do |real_obj|
-
yml += "#{real_obj.name.downcase.gsub(' ','_').gsub('-','')}:\n"
-
real_obj.attributes.each do |k,v|
-
if k =~ /_id$/ && real_obj.respond_to?(k.gsub(/_id$/,'').to_sym) && ! v.nil?
-
n_klass = k.gsub(/_id$/,'').camelize.constantize.find(v) rescue nil
-
key = k.gsub(/_id$/,'')
-
val = n_klass.name.downcase.gsub(' ','_').gsub('-','')
-
yml += " #{key}: #{val}\n"
-
else
-
yml += " #{k}: #{v}\n" unless k.to_s == 'id'
-
end
-
end
-
end
-
yml
-
end
The use is:
RUBY:
-
require 'util'
-
print Util.real_yml(Corporation)
Copy/paste into corporations.yml
So far it's working great!

