Creating a Seeds File
In this lesson we'll clean up the data that we're working with in the local database and walk through how to create a seeds file.
Guide Tasks
  • Read Tutorial

Our local data is starting to cause issues, it's getting hard to test features in the browser because we have quite a few nil values in the database. We've already taken care of this issue from a code perspective by implementing validations and tests, however that doesn't fix our pre-existing database values. This is a very normal issue for young rails applications, so let's walk through how to fix it.

You may notice that this lesson doesn't specifically deal with the post feature, so let's run:

git checkout master

This will take us back to the code stage before we started the post feature, so it would be a bad practice to build our seeds file on this branch. And this is a good time to practice working with multiple git branches at the same time. In the real world you will be called on to work on patches and quick features while you're in the middle of other features, this is why it's important to isolate code on branches. With that in mind let's create a branch for our seeds file:

git checkout -b add-seeds

This will isolate our work on the seeds file. So what is a seeds file? Rails ships with the ability to quick add data to the database via the db/seeds.rb file. This is a plan ruby file that allows you to execute commands to fill your database (and clear out old data). This is great during development, but disastrous on a production environment, so only use these commands on your local development environment. Let's open the file and fill it with a simple command:

# db/seeds.rb

Topic.create!(title: "My Title")

Now if you run the command (and as a warning, this command wipes out all records on your local system, so be warned):

rake db:setup

It will clear out the database, update the database tables and run the seeds file. We can test this out by starting up the rails console and running the command:

Topic.all.count

This will return a value of 1, running Topic.last will show that the topic in the database is our topic from the seeds file. Now that we know that this is working, let's create a more robust seeds file.

# db/seeds.rb

puts "Starting to run seeds file..."

100.times do |topic|
  Topic.create!(title: "My Title #{topic}")
end

puts "100 Topics created"

User.create!(
  email: "admin@test.com",
  password: "asdfasdf",
  password_confirmation: "asdfasdf",
  first_name: "Jon",
  last_name: "Snow",
  username: "wallwatcher",
  role: "admin"
)

puts "Admin user created"

User.create!(
  email: "student@test.com",
  password: "asdfasdf",
  password_confirmation: "asdfasdf",
  first_name: "Jon",
  last_name: "Snow",
  username: "youngwallwatcher",
  role: "student"
)

puts "Student user created"

Here I'm creating 100 topics, and two users with different roles and printing these values out to the console. Now if I run rake db:setup it will run this seeds file and fill out database with the new values.

Let's merge this branch with the master branch and then update our add-posts branch with our changes. Run:

git add .
git ci -m 'Added a seeds file'
git checkout master
git merge add-seeds
git push

Now that our changes are merged with the master branch and pushed up to the repo we need to include our changes in the branch we've been working on. We can do this by following the steps below, checkout the add-posts branch:

git checkout add-posts

Then to get the latest version of the master branch, run:

git pull origin master

This will pull down the changes into our add-posts branch and will ask you to give an explanation for the merge. You can simply run :wq since this doesn't require a description. Now if you open your codebase you'll see we have all of our post feature code and we also have our new seeds file. Let's update the seeds file so we can also have some posts to work with, let's add the following at the bottom of the seeds file:

# db/seeds.rb

50.times do |post|
  Post.create!(
    title: "My Post #{post}",
    content: "Some amazing content here",
    topic_id: Topic.last.id,
    user_id: User.last.id
  )
end

puts "50 posts were created"

Running rake db:setup will wipe out the database and add in our new posts. We now have a clean data setup to work with in the browser. We can test this out by starting up the rails server and going to http://localhost:3000/topics/my-title-99/posts and everything is now working without any errors. Nice work! Now we can work with the rest of the post feature.

Resources