Error when trying to populate database with sample microposts

  • Question
  • Updated 1 year ago
  • Answered
From section 11.2.2 Sample Posts

After updating the sample_data.rake file to include sample microposts, I run "rake db:populate" and get this error response:

rake aborted!
Validation failed: Email has already been taken

Do I need to somehow clear my sample database before trying to populate it with sample microposts?
Photo of Brennan

Brennan

  • 9 Posts
  • 0 Likes
  • sad

Posted 4 years ago

  • 1
Photo of Rails Tutorial

Rails Tutorial, Official Rep

  • 1009 Posts
  • 112 Likes
It would do you a disservice to help too much with this, since this is the kind of problem web developers run into all the time. What I can tell you is that the sample data rake task in the book is right, and that it does clear the database. Re-check your code and see if you can figure out what the problem is.
Photo of Brennan

Brennan

  • 9 Posts
  • 0 Likes
By the way, I love that you just don't give me the answer and make me dig harder. I'm also amazed at how quickly the official reps respond.

Anyway, I figured it out. I had duplicated a block of code. I'm not sure why that wasn't obvious the first half hour I stared at it, ha.

I do want to point out what I think is a separate issue though with the sample_data.rake code in Listing 11.20. It shows 4 "end"s at the end, but I actually had to include 5 "end"s otherwise I got a syntax error "unexpected $end, expecting kEND"

Is this an instance where I need to re-check my code or is it a typo?
Photo of Robert JewellRJ

Robert Jewell

  • 1 Post
  • 0 Likes
Hey Brennan, what was the duplicated block of code and in what file was it? I'm running into the same problem.
Photo of Rails Tutorial

Rails Tutorial, Official Rep

  • 1009 Posts
  • 112 Likes
Glad I could help. My "official reps" are me. :-)

I think the number of "end"s is right, since you can watch them cascade one at a time. But as long as you don't get a syntax error, your code is good to go.

P.S. I don't always respond this quickly. Often it will take a week or two to get a reply. Just so you know. :-)
Photo of PhillP

Phill

  • 1 Post
  • 0 Likes
I ran into the same problem with not having enough "end"s in my file.

When I went back through the code I realized I did need 5 "end"s however the 5th end needed to be placed above the code that makes the microposts. Otherwise I had a loop that would run 99 times of 50 microposts netting me 4950 microposts instead of 50.
Photo of ansorensenA

ansorensen

  • 1 Post
  • 0 Likes
I ran into this problem, too, a little earlier. My issue was duplicate code in the sample_data.rake that's created in listing 9.41... I added to the file without realizing that was supposed to delete the earlier code in 9.30.

Thanks, Brennan for giving me a hint in your response. Hope this helps anyone else who needs a little more of hint.