Created
September 15, 2009 16:40
-
-
Save ambethia/187440 to your computer and use it in GitHub Desktop.
Using environment specific yaml fixtures as seed data in Rails 2.3.4
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
# == Using environment specific yaml fixtures as seed data in Rails 2.3.4 | |
# | |
# Setup your directory structure like below: | |
# | |
# -- RAILS_ROOT | |
# `-- db | |
# |-- seeds | |
# | |-- development | |
# | | |-- books.yml | |
# | | `-- users.yml | |
# | `-- production | |
# | `-- users.yml | |
# `-- seeds.rb | |
# | |
# And put this block in your `seeds.rb` file: | |
# | |
Dir[Rails.root.join('db', 'seeds', Rails.env, '*.yml')].each do |seed| | |
documents = YAML.load_file(seed) | |
klass = File.basename(seed, '.yml').classify.constantize | |
documents.each do |doc| | |
klass.create(doc.last) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment