Rails How to Create Data Schema Seed Data

Add seed data to development database for testing

This is what the db/seeds.rb file is for.

You can execute it with rake db:seed

The default contents of seeds.rb

# 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 => 'Daley', :city => cities.first)

You can use ruby in this, so to insert 10 users:

1.upto(10) do |i|
User.create(:name => "User #{i}")
end

How to seed database with has_and_belongs_to_many

If you want to associate each Phone instance with all the categories you can do it like so:

Category.create(name:'tactile-screen')
Category.create(name:'mobile')
Category.create(name:'landline')

ids = Category.all.ids

Phone.create(name:'Home', phone:'+00 0 00 00 00 00', category_ids: ids)
Phone.create(name:'Work', phone:'+00 1 00 00 00 00', category_ids: ids)

Otherwise just create arrays of the records and you can do something like the following if you for example want to apply a random category:

categories = ['tactile-screen', 'mobile', 'landline'].map do |c|
Category.create(name: c)
end

phones = [
Phone.create(name:'Home', phone:'+00 0 00 00 00 00'),
Phone.create(name:'Work', phone:'+00 1 00 00 00 00')
]

phones.each do |p|
p.categories << categories.sample
end

If you are using Faker or FFaker you can use this nifty trick to generate random records:

require 'ffaker'

# generate a 100 random numbers
phones = 100.times.map do
Phone.create(name:'Home', FFaker::PhoneNumber.phone_number)
end

# generate between 0 and 100 random numbers
phones = ((rand * 100).floor).times.map do
Phone.create(name:'Home', FFaker::PhoneNumber.phone_number)
end

How to add new seed data to existing rails database

Structure your seed.rb file to allow ongoing creation and updating of data. You are not limited to running a seed file only once and if you think it's only used for initial deployment you will miss out on the flexibility it can offer in setting reference data.

A seed file is just ruby so you can do things like:

user = User.find_or_initialize_by(email: 'bob@example.com')
user.name = 'Bob'
user.password = 'secret'
user.role = 'manager'
user.save!

This will create new data if it doesn't exist or update the data if it finds some.

If you structure your seed file correctly you can also create and update dependent objects.

I recommend using the bang save to ensure that exceptions are raised in the event that an object cannot be saved. This is the easiest method of debugging the seed.

I use the seedbank gem to provide more structure to my seed data, including setting data per environment, dependent seeds and more.

I don't recommend using migrations for seed data. There is a lack of flexibility (how do you target seed data to just one environment for instance) and no real way to build up a reusable set of data that can be run at any time to refresh a particular environment. You would also have a set of migrations which have no reference to your schema and you would have to create new migrations every time you wanted to generate new or vary current data.

Can't create data with rails db:seed

There is an error in your model file. The validations are specified with symbols, which start with a : character that you have omitted. They should look like this:

class State < ApplicationRecord
validates :country_code, presence: true
validates :name, presence: true, uniqueness:true
end

How do I create a seed file using a model with references? I'm not sure I set up the models correctly

Hardcoding ids is not a good practice (even when your db is empty - new record is not guaranteed to have id 1),

In your seeds - pass objects, like:

lana = User.create(user_name: "Lana", email: "lanagrebnova@gmail.com", password: "bony2015", street_address: "15 Schley Rd.", city: "Far Hills", state: "NJ", balance: 25 )
burrito = Item.create(name: "burrito", price: 8, vendor: lana)


Related Topics



Leave a reply



Submit