I need to fill my database with data from yaml file, which contains something like:
-Users:
------Users_nameship: 'First group'
------Members:
----------Name:'Jack'
etc. And i need to use seeds.rb file For now i make something like that:
require 'yaml'
seed_file = Rails.root.join('db', 'seeds.yml')
@config = Hash.new(YAML::load_file(seed_file))
@config.each do |key, values|
values.each do |k,v|
Project.create title:v
end
end
But its dont fill any cell in table. Please help
Yaml files are used by fixtures for testing purposes. On the other hand, you can use db/seed.rb to populate your db with the following command:
rake db:seed
Check the following models:
class Group < ApplicationRecord
has_many :users
end
class User < ApplicationRecord
belongs_to :group
end
You could generate two groups with two users each with the following seed.rb file:
Group.create!([{name: "First group"}, {name: "Second group"}])
User.create!([{name: 'Jack', group_id: 1}, {name: 'Bob', group_id: 1}, {name: 'Robert', group_id: 2}, {name: 'John', group_id: 2}])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.