Sample microposts are failing to appear like in Figure 10.6 Ch 10.2.2 Rails Tutorial, nothing is showing up

StackOverflow https://stackoverflow.com/questions/21175062

  •  28-09-2022
  •  | 
  •  

Question

I have input all of the correct code from the chapter up until now and my Rspec tests are passing. I have reset and repopulated the database as per the instructions. However, while I should be seeing something like Figure 10.6 when I load /users/1, what I am seeing looks exactly like figure 10.5

My lib file is posted below, let me know if I could post any other code that might help to elucidate my issue?

namespace :db do
desc "Fill database with sample data"
task populate: :environment do

    users = User.all(limit: 6)
    50.times do
        content = Faker::Lorem.sentence(5)
        users.each { |user| user.microposts.create!(content: content) }
    end
    admin = User.create!(name: "Example User",
                 email: "example@awesome.org",
                 password: "foobar",
                 password_confirmation: "foobar",
                 admin: true)
    99.times do |n|
        name = Faker::Name.name
        email = "example-#{n+1}@railstutorial.org"
        password = "password"
        User.create!(name:                  name,
                     email:                 email,
                     password:              password,
                     password_confirmation: password)
    end
end
end

Thanks, and let me know if you have run into a similar issue?

Was it helpful?

Solution

Because you need to make users first.

namespace :db do
  desc "Fill database with sample data"
  task populate: :environment do
    admin = User.create!(name: "Example User",
                         email: "example@railstutorial.org",
                         password: "foobar",
                         password_confirmation: "foobar",
                         admin: true)

    99.times do |n|
      name  = Faker::Name.name
      email = "example-#{n+1}@railstutorial.org"
      password  = "password"
      User.create!(name: name,
                   email: email,
                   password: password,
                   password_confirmation: password)
    end
    users = User.all(limit: 6)
    50.times do
      content = Faker::Lorem.sentence(5)
      users.each { |user| user.microposts.create!(content: content) }
    end
  end
end

OTHER TIPS

The active record method all doesnt accept any parameters. so change the

users = User.all(limit: 6)

to:

users = User.limit(6)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top