Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

How to seed database using the YAML file?

I need to make a script for seed.rb so that it can load the information from YAML file using hash and add it to database

I tried to load seed.yml by HashWithIndifferentAccess but I don`t know if it works and also I have no idea how to seed database with this hash

//that`s my seed.yml file

 projects:
   - title: 'family'
     todos:
        - text: 'Get some milk'
          isCompleted: false
        - text: 'Cook some bacon'
          isCompleted: true
        - text: 'Repair the front door'
          isCompleted: false
   - title: 'work'
     todos:
        - text: 'Call my boss'
          isCompleted: true
        - text: 'Finish my work tasks'
          isCompleted: true

//and what I tried in my seed.rb

seed_file = Rails.root.join('db', 'seeds', 'seeds.yml')
config = HashWithIndifferentAccess.new(YAML::load_file(seed_file))

//also there models

class Project < ActiveRecord::Base 

has_many :todos 

end 

class Todo < ActiveRecord::Base 

belongs_to :project 

end 

//and migrations

create_table "projects", force: :cascade do |t| 
t.string "title" 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
end 

create_table "todos", force: :cascade do |t| 
t.string "text" 
t.boolean "isCompleted" 
t.integer "project_id" 
t.datetime "created_at", null: false 
t.datetime "updated_at", null: false 
end 

So I need to fill the database so that I can get data from the seed.yml it should work in rails console by Project.all and Todo.all commands

Comments