DEV Community

Cover image for Understanding Rails Seeds
Nikhil Devarasetty
Nikhil Devarasetty

Posted on

Understanding Rails Seeds

What Are Seeds?

In a Ruby on Rails application, seeds are used to populate the database with predefined data. This data could include default user accounts, sample products, or any other records required for the application to function correctly. Seeds are especially useful during the development and testing phases, allowing developers to work with a database that contains meaningful data.

Configuring Seeds:

Configuring seeds involves creating a file named seeds.rb in the Rails application's db directory. This file contains Ruby code that specifies the data to be inserted into the database. The seeds.rb file is structured similarly to a Rails migration file but is focused on data rather than schema changes.

To run the seeds and populate the database, use the following command:

rails db:seed
Enter fullscreen mode Exit fullscreen mode

How to Use Seeds:

Create the seeds.rb File:

Start by creating the seeds.rb file in the db directory of your Rails application.

Write Seed Data:

Inside seeds.rb, use Ruby code to define the data you want to insert. This may include creating instances of ActiveRecord models and populating their attributes.

# db/seeds.rb

User.create(username: 'admin', email: 'admin@example.com', password: 'password')
Product.create(name: 'Sample Product', price: 19.99, description: 'A description of the product.')
Enter fullscreen mode Exit fullscreen mode

Run the Seeds:

Execute the following command to populate the database with the defined seed data:

rails db:seed
Enter fullscreen mode Exit fullscreen mode

Other commands to work around:

  • Generate Seeds File: To create a new seeds file (users_seeds.rb), you can use the following command:
rails generate seed users_seed.rb
Enter fullscreen mode Exit fullscreen mode

This command will generate an empty db/users_seed.rb file if it doesn't exist.

  • Run Seeds: To execute the seeds and populate the database with the defined data in the seeds.rb file, use:
rails db:seed
Enter fullscreen mode Exit fullscreen mode

This command will run the code inside the seeds.rb file, creating records in the database.

  • Reset and Seed Database: If you want to reset the database (drop, create, and migrate) and then run the seeds, you can use the following command:
rails db:reset
Enter fullscreen mode Exit fullscreen mode

This command is helpful during development when you need to recreate the database with the latest schema and seed data.

  • Specific Seed File: If you have multiple seed files or a custom seed file, you can specify the file to run:
rails db:seed:custom_seeds
Enter fullscreen mode Exit fullscreen mode

Replace custom_seeds with the actual filename you want to execute.

  • Loading Seed Data in Development Environment: During development, you might want to run seeds frequently. You can use the following command to load the seeds:
rails db:seed RAILS_ENV=development
Enter fullscreen mode Exit fullscreen mode
  • Running Custom Seed Tasks: You can create custom seed tasks in your lib/tasks directory and run them using:
rails db:seed:custom_task
Enter fullscreen mode Exit fullscreen mode

Example lib/tasks/custom_seed.rake:

namespace :db do
  namespace :seed do
    desc 'Custom seed task'
    task custom_task: :environment do
      # Your custom seed logic
    end
  end
end
Enter fullscreen mode Exit fullscreen mode

Pros of Using Seeds:

  • Data Consistency: Seeds provide a consistent set of initial data for all developers working on the project, ensuring a standardized environment.

  • Faster Development: Seeds help developers set up a working database quickly, saving time during the development process.

  • Testing: Seeds are invaluable for testing, allowing developers to create predictable scenarios and reproduce specific conditions for testing purposes.

Cons of Using Seeds:

  • Static Data: Seeds provide static data that might not evolve with changes in the application. Developers need to update seeds manually when the data model changes.

  • Security Concerns: Care must be taken not to include sensitive information in seed data, especially if the seeds file is committed to version control.

  • Maintenance Overhead: As the application evolves, maintaining and updating seed data may become a task, especially in larger projects.

Top comments (0)