<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: fixtures code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 02:13:07 GMT</pubDate>
    <description>DZone Snippets: fixtures code</description>
    <item>
      <title>Synchronizing Rails DB Contents via Fixtures</title>
      <link>http://snippets.dzone.com/posts/show/3393</link>
      <description>The following rake task will dump the contents of the current environment's database to YAML fixtures. Stick the following in lib/tasks/fixtures.rake:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :db do&lt;br /&gt;  namespace :fixtures do&lt;br /&gt;    &lt;br /&gt;    desc 'Create YAML test fixtures from data in an existing database.  &lt;br /&gt;    Defaults to development database.  Set RAILS_ENV to override.'&lt;br /&gt;    task :dump =&gt; :environment do&lt;br /&gt;      sql  = "SELECT * FROM %s"&lt;br /&gt;      skip_tables = ["schema_info"]&lt;br /&gt;      ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;        i = "000"&lt;br /&gt;        File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;          data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;          file.write data.inject({}) { |hash, record|&lt;br /&gt;            hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;            hash&lt;br /&gt;          }.to_yaml&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After making changes to the database that you'd like to dump to fixtures:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:fixtures:dump&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After checking out updated fixtures from SVN:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:migrate&lt;br /&gt;rake db:fixtures:load&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 31 Jan 2007 01:04:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3393</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
  </channel>
</rss>
