How do I use a YAML file instead of seeds.rb to load the initial data into a database?
Add code in db/seeds.rb to parse the YAML file, e.g.:
seed_file = Rails.root.join('db', 'seeds', 'categories.yml')
config = YAML::load_file(seed_file)
Category.create!(config)
Then, simply place the YAML fie in db/seeds/categories.yml. The YAML file should be a list of associative arrays, e.g.:
- name: accessory
  shortcode: A
- name: laptop
  shortcode: L
- name: server
  shortcode: S
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With