You need to create a seeds.rb script that can write information to the database from the seeds.yml file. Please tell me how to do this? The question is taken from the task http://webnmobile.tumblr.com/post/122866067493/2-mvc-with-ruby-on-rails . Please help to understand this issue. I searched for information for several days, but did not find anything that could help me. thank
- That is, you got into Google “rails seeds” and for some reason the first three links in the search results didn't help you at all? Why? - andreymal
- Did not help unfortunately. The code stubbornly does not want to work. - 2monthy_developer
- Show the error code and texts in this case - andreymal 8:25 pm
|
1 answer
Assuming you have:
Project
model withtitle
field andhas_many :todos
associationhas_many :todos
Todo
model with fieldstitle
andisCompleted
andbelongs_to :project
association
something like this:
# db/seeds.rb require 'yaml' # Парсим данные из yml файла data = YAML.load_file('seeds.yml') # Получаем массив проектов (получается массив хешей) projects = data[:projects] # Для каждого проекта из массива: projects.each do |project| # Создаем новый проект в БД created_project = Project.create!(title: project[:title]) # Создаем для нового проекта todos created_project.todos.create!(project[:todos]) end
|