Rails has a standardization to popular your database, this standardization serves to maintain the maintenance of your app. I don’t know what your database columns look like, so I’ll post a generic template:
Inside your Rails project folder, you will have the following file: db/seed.rb
, this is the file responsible for popular your database.
You can enter data as follows:
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
##
Departament.create(name: 'Poltronas', subtitle:'', slug: 'poltronas', description: '')
Departament.create(name: 'Puffs', subtitle:'', slug: 'puffs', description: '')
Departament.create(name: 'Sofás', subtitle:'', slug: 'sofas', description: '')
Departament.create(name: 'Chaises', subtitle:'', slug: 'chaises', description: '')
Departament.create(name: 'Banquetas', subtitle:'', slug: 'banquetas', description: '')
Departament.create(name: 'Office', subtitle:'', slug: 'office', description: '')
Departament.create(name: 'Cadeiras', subtitle:'', slug: 'cadeiras', description: '')
Departament.create(name: 'Mesas', subtitle:'', slug: 'mesas', description: '')
Departament.create(name: 'Itens', subtitle:'', slug: 'itens', description: '')
Departament.create(name: 'Pronta Entrega', subtitle:'', slug: 'pronta-entrega', description: 'Conheça nossos produtos disponíveis a pronta entrega')
You can also use iterations in it, example:
5.times do |i|
Product.create(name: "Product ##{i}", description: "A product.")
end
To apply your popularization just enter your terminal in the folder of your project with the command:
rake db:seed
There are people who include these popularizations in Migrations
also...
class AddInitialProducts < ActiveRecord::Migration
def up
5.times do |i|
Product.create(name: "Product ##{i}", description: "A product.")
end
end
def down
Product.delete_all
end
end
Mark your answer as correct to put it first and indicate to users that this is the answer that answers the question.
– user7261