How to generate fixtures based on my development database?

The question is old, but as it still seems relevant: yes, there is an easy way to create fixtures from your development database:

class ActiveRecord::Base
  def dump_fixture
    fixture_file = "#{Rails.root}/test/fixtures/#{self.class.table_name}.yml"
    File.open(fixture_file, "a+") do |f|
      f.puts({ "#{self.class.table_name.singularize}_#{id}" => attributes }.
        to_yaml.sub!(/---\s?/, "\n"))
    end
  end
end

Place this in a file in config/initializers – now you can dump any ActiveRecord object in your Rails console and it will automatically be appended at the end of it’s respective fixture file:

User.first.dump_fixture appends fixture data to test/fixtures/users.yml.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)