Seeding Data Efficiently in Rails – Turing



Seeding Data Efficiently in Rails – Turing

0 0


class-seed-data-rails


On Github itechdom / class-seed-data-rails

Seeding Data Efficiently in Rails

Turing

Osama Alghanmi / @itechdom

Learning Goals

  • Writing seed files: the manual way
  • Why seeding efficiently matters
  • When to use seeds during the development process
  • Writing seed files: the better way
  • How to use a Faker
  • Exercise: Seed your pivot

Structure

  • 10 - Building an Inefficient Seed File
  • 10 - Lecture: Why Seeding Efficiently Matters
  • 10 - When to seed during the dev process
  • 5 - Break
  • 10 - Writing seed files: the better way
  • 10 - Using Faker
  • 5 - Recap
  • 25 - Exercise: seed your pivot

Writing seed files: the manual way

  • Setting up everything up by hand, which includes

    • Entering data in multiple lines instead of loops:

      user_1  = User.create(name: "Richard", email: "richard@example.com", password: "asdf")
      user_2  = User.create(name: "Sam", email: "sam@example.com", password: "asdf")
      user_3  = User.create(name: "Sam", email: "sam@example.com", password: "asdf")
    • You set up relationships by hand:

      order_2 = user_2.orders.create(amount: 10.00)
      order_2.items << item_2
      puts "Created Order For: #{order_2.user.name}"

Why seeding efficiently matters

  • Easier to maintain: you don't have to spend a long time updating as you migrate.

When is the best time to Seed

  • Update them as you create new migrations, This saves you from going back and editing the whole seed file

Writing seed files: the better way

  • Let's go back to our example and look at how we can improve the way we write seed files:

    • Use loops instead of writing things in multiple lines:

      10.times do |i|
      user  = User.create(name: "user ##{i}", email: "email#{i}@example.com", password: "123")
      puts "Created User: #{user.name}"
      end
    • Setup Relationships using:

  def generate_orders
    100.times do |i|
      user  = User.order("RANDOM()").limit(1).first
      user.orders.create(amount: i*2) do |order|
        rand(1..10).times do
          order.items << Item.order("RANDOM()").limit(1).first
        end
      end
      puts "Created Orders For: #{user.name}"
    end
  end

How to use Faker

  • We saw earlier that we are adding names by using "i" in our loops.
      10.times do |i|
        user  = User.create(name: "user ##{i}", email: "email#{i}@example.com", password: "123")
        puts "Created User: #{user.name}"
      end
  • There's a nice gem called Faker which allows us to generate, randomly, different values for different columns.
  • https://github.com/stympy/faker
  • Adds more flavor to your seed file.

Let's Faker it

  • In our previous example, let's convert our seed file to use Faker

    • Let's use Faker to generate user's names:
  def generate_users
    10.times do
      user  = User.create(name: Faker::Name.name, email: Faker::Internet.email, password: "asdf")
      puts "Created User: #{user.name}"
    end
  end
* Setup Relationships using Faker:
  def generate_orders
    100.times do
      user  = User.order("RANDOM()").limit(1).first
      user.orders.create(amount: Faker::Commerce.price) do |order|
        rand(1..10).times do
          order.items << Item.order("RANDOM()").limit(1).first
        end
      end
      puts "Created Orders For: #{user.name}"
    end
  end

Recap

  • Writing seed files: the manual way
  • Why seeding efficiently matters
  • When to use seeds during the development process
  • Writing seed files: the better way
  • How to use a Faker

Exercise: Seed your Pivot

  • Use Loops
  • Use Faker