Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Adebola
Adebola

Posted on

     

How to seed your rails database with faker

I built a Customer Support System a few days ago and started to think of a way to seed my rails database with random data for testing without having to manually type in the data. This may not be the best way to do it, but it works and allows you to test your application.

We want a way to generate users that have tickets and also have comments without having to manually type in a value for each field.
Let's start with the basic schema of the DB.

Schema for rails database

Our Database, schema and migrations have already been generated and the main focus here will be on seeding the database.

First, install thefaker gem by adding it to your list of gems in your gemfile.

gem 'faker'

then runbundle install

Once done, head into your seeds.rb file inside the db directory. In here, require the faker gem at the beginning using

require 'faker'

Next, let's createUsers using fake data from thefaker gem.

# generate 20 users(1..20).each do |id|    User.create!(# each user is assigned an id from 1-20        id: id,         name: Faker::Name.name,        email: Faker::Internet.email,# issue each user the same password        password: "password",         password_confirmation: "password",# a user can have only one of these roles        role: %w[customer admin agent].sample     )end
Enter fullscreen modeExit fullscreen mode

The Faker attributes you can use are all available from thefaker GitHub page.

role: %w[customer admin agent].sample

The line above may look a little strange - we've designed our model to only accept one of the values in ["customer", "admin", "agent"] as the values assigned to role for users.

Calling .sample on the array(a neat ruby method) causes the code to select one of the three possible values.

I've added some comments using the# symbol lines.

Next, let's createTickets. Remember that eachticket belongs to aUser. As such, we'll need to find a way to assign tickets to users randomly.

# create 10 tickets in DB(1..10).each do |id|    Ticket.create!(        id: id,# we have userIds between 1 and 20. Assign a ticket to a user # randomly        user_id: rand(1..20),         title: Faker::University.name,         status: %w[opened in_progress completed].sample,# generate a fake paragraph        request: Faker::Lorem.paragraphs     )end
Enter fullscreen modeExit fullscreen mode

user_id: rand(1..20)

The line above is used to generate a random number between 1 and 20. Recall that our users were assigned Id's between 1 and 20.
Finally, let's generate comments that belong to a User and is tied to a ticket.

# create comments in DB. Comments belong to tickets(1..10).each do |id|    Comment.create!(        id: id,        user_id: rand(1..20),        comment: Faker::Lorem.sentence(word_count: 3),        user_name: User.find(rand(1..20)).name,        ticket_id: rand(1..10),        user_role: %w[customer admin agent].sample    )end
Enter fullscreen modeExit fullscreen mode

Once done, simply run

rails db:seed

You can check that everything went well by checking your data within the rails console.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Chief of Staff @Moni.africa (YC W22) | In love with Nigerian Jollof and plantain. 🇳🇬
  • Location
    Nigeria
  • Education
    Microverse / University of Helsinki / Eastern Mediterranean University
  • Work
    moni
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp