DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • What Are SpeedUp and ScaleUp in DBMS?
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • Improving Backend Performance Part 1/3: Lazy Loading in Vaadin Apps
  • Using Slash GraphQL to Create InstaMeme—A Meme Sharing App

Trending

  • Spring Boot Done Right: Lessons From a 400-Module Codebase
  • Solving the Mystery: Why Java RSS Grows in Docker on M1 Macs
  • Real-Time AI Inference at Scale Using Cloud Run, GPUs, and Vertex AI
  • Beyond Conversation: Mastering Context with Claude Code Skills and Agents
  1. DZone
  2. Data Engineering
  3. Data
  4. Rake Tasks: db:seed vs. dev:prime

Rake Tasks: db:seed vs. dev:prime

Using rake db:seed, which is already built into Rails, may seem preferable to using a custom rake task like rake dev:prime. However there's a time and place for both.

By 
Jason Draper user avatar
Jason Draper
·
Sep. 15, 16 · Opinion
Likes (3)
Comment
Save
Tweet
Share
4.2K Views

Join the DZone community and get the full member experience.

Join For Free

In suspenders, we have a rake task called dev:prime that allows us to seed the database with information. We’re often asked why we prefer a custom task over using rake db:seed, which is already built into Rails. Let’s talk about a few of the differences.

Rake db:seed

We reserve the db:seed tasks specifically for data that must be present for your application to function in any environment. One example of this may be a list of U.S. states in your database that your address form relies on. That data should always exist, whether the app is being used in development or production.

Rake dev:prime

Working on an app is easier if you have data that looks and feels similar to what your users see. If you’re new to the codebase or new to a specific feature, having data preloaded that makes sense and sets you up for the feature, is really helpful.

Our development seeds contain data that are necessary for users to view most of the features of the app. They’re very convenient for developers or designers who are running the app locally. If you were building a multi-user blogging application, your seeds file would likely generate the following:

  • An admin user
  • 2-3 normal users
  • Enough posts to ensure you have pagination (most likely, you’d want to space out their published date enough that you could also test the features to view posts by month or year)
  • 5-10 comments on each post with various authors including anonymous authors
  • 2-3 deleted posts

As you’re building a new feature, if it requires special data to setup, put it directly into the seeds file instead of adding it with the web interface or the console. For example, if you’re building a feature to make sure that posts without a published_at value are not visible on the homepage, add that to your development seeds instead of creating one through the UI. This ensures that the next person who needs to test that feature has it ready to go without as much work.

How to Generate Data

For database seeds, we do not recommend using FactoryGirl to generate your data. For generating data for your local development environment, however, it can be very helpful to leverage your test factories to simplify setup. For building a lot of blog posts you could rely on the defaults for most fields but randomize a few key fields:

if Rails.env.development? || Rails.env.test?
  require "factory_girl"

  namespace :dev do
    desc "Sample data for local development environment"
    task prime: "db:setup" do
      include FactoryGirl::Syntax::Methods
      titles = [
        "You won't believe what we found out about cheese!",
        "Don't skip these 12 super foods",
        "Only 20s kids will remember these toys",
      ]

      authors = [
        "Liz",
        "Sam Seaborn",
        "The Honorable 3rd Duke of Long Names",
      ].map do |name|
        create(:user, name: name)
      end

      50.times do
        create(
          :post,
          author: authors.sample,
          title: titles.sample,
          published_at: (1..365).to_a.sample.days.ago,
        ).each do |post|
          (1..10).to_a.sample.times do
            create(:comment, post: post)
          end
        end
      end

      create_list(:post, 3, deleted_at: (1..10).to_.sample.days.ago)
    end
  end
end

It’s All About Communication 

Development seeds are another form of communication. Reading the tests can expose a great deal of information about an app and your development seeds can provide a similar benefit. If you treat them with care, they can provide a great way to onboard new teammates as well make feature development and bug fixes easier for your existing teammates.

Task (computing) Data (computing) app POST (HTTP) Testing Multi-user application Database

Published at DZone with permission of Jason Draper. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • What Are SpeedUp and ScaleUp in DBMS?
  • How to Build a Full-Stack App With Next.js, Prisma, Postgres, and Fastify
  • Improving Backend Performance Part 1/3: Lazy Loading in Vaadin Apps
  • Using Slash GraphQL to Create InstaMeme—A Meme Sharing App

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook