Created
November 5, 2010 10:59
-
-
Save cwise/663978 to your computer and use it in GitHub Desktop.
Generate a seeds.rb from existing data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# given a model (SomeModel), generate seeds.rb file from existing data in the database | |
elements=SomeModel.all.collect do |m| | |
"#{m.class.to_s}.create(#{m.attributes.select{|k, v| !["id", "created_at", "updated_at"].include?(k)}.collect{ |k, v| ":#{k} => '#{v.to_s.gsub("'", "\\\\'")}'" }.join(', ')})" | |
end | |
File.open('seeds.rb', 'w') {|f| elements.each{|element| f.puts(element)} } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment