1

I'm currently going through the RoR guides, and i'm stuck at...

"Adding following/follower relationships to the sample data."

Here's the code that is suppose to work: sample_app/lib/task/sample_data.rake

namespace :db do
desc "Fill database with sample data"
task populate: :environment do
    make_users
    make_microposts
    make_relationships
  end
end

def make_users
 admin = User.create!(name:     "Example User2",
                     email:    "[email protected]",
                     password: "foobar",
                     password_confirmation: "foobar")
admin.toggle!(:admin)
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

def make_microposts
  users = User.all(limit: 6)
  50.times do
    content = Faker::Lorem.sentence(5)
    users.each { |user| user.microposts.create!(content: content) }
  end
end

def make_relationships
  users = User.all
  user  = users.first
  followed_users = users[2..50]
  followers      = users[3..40]
  followed_users.each { |followed| user.follow!(followed) }
  followers.each      { |follower| follower.follow!(user) }
end

when i do rake db:reset my database reset with no problems.

when i do rake db:populate an error occurred stating this:

 rake aborted!
 Validation failed: Follower can't be blank`

so i checked my database, and all tables were populated except for "relationships" table.. any thoughts or suggestions? I'm pretty sure there's a problem with the code, def making_relationships, to be exact. hope anyone has a solution to this..

-Marc

1 Answer 1

3

Since you're calling .create! on models like User and Micropost (user.microposts), it is one of them throwing the error mentioned.

Please, post the code for these models to enable us answer more specifically.

You can still debug the problem by yourself though. Just hit rails c in the projects root directory, and try to create instances with the very same attributes you're trying in rake task:

$ rails c

$ user = User.create!(name:     name,
                      email:    email,
                      password: password,
                      password_confirmation: password)

$ micropost = user.microposts.create!(content: "Hello, cruel world!")

# by this step you should already see some errors raised; if that's not sufficient,
# call the following methods to figure out what model suffers the validation error
user.errors.full_messages
micropost.errors.full_messages

Anyway, it's the validation that's not satisfied. Double check you're passing all required attributes passed when creating a model with a shebang create!. Specifically check which model requires presence of Follower (whatever that is).

Sign up to request clarification or add additional context in comments.

13 Comments

i tried the console it displays that no errors where found. " [] " was shown in both error finding methods. as for the follower code, it lies in the relationship model and user model. here's a link for the gist of the entire code: gist.github.com/2768605
@Marc, try running "rake db:populate --trace" to determine what exact line throws the error.
From the User model code I'm looking at it seems that creating followers doens't work well. As stated previously, try invoking console and running something like user = User.first; user.follow!(User.last)
i tried your task @gmile with using your code for the console. it shows the same error i receive in my git. here's the output of it.. gist.github.com/2774192 -- i tried looking into some of the files manually by going through the files but it's out of my knowledge and comprehension on how to solve this validation problem.. or whatever problem this may be
Right now it's the Validation failed: Email has already been taken. It seems that you're trying to create a user which already exists, which is possible if you ran the script multiple times already without dropping the users table
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.