Skip to content

Instantly share code, notes, and snippets.

@JoseJRVazquez
Created April 28, 2014 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JoseJRVazquez/11375777 to your computer and use it in GitHub Desktop.
Save JoseJRVazquez/11375777 to your computer and use it in GitHub Desktop.
A seed.rb file
require 'faker'
# Create Users
5.times do
user = User.new(
name: Faker::Name.name,
email: Faker::Internet.email,
password: Faker::Lorem.characters(10)
)
user.skip_confirmation!
user.save
end
users = User.all
# Create Topics
15.times do
Topic.create(
name: Faker::Lorem.sentence,
description: Faker::Lorem.paragraph
)
end
topics = Topic.all
# Create Posts
50.times do
Post.create(
user: users.sample,
topic: topics.sample,
title: Faker::Lorem.sentence,
body: Faker::Lorem.paragraph
)
end
posts = Post.all
# Create Comments
100.times do
Comment.create(
# user: users.sample, # we have not yet associated Users with Comments
post: posts.sample,
body: Faker::Lorem.paragraph
)
end
# Create an admin user
admin = User.new(
name: 'Admin User',
email: 'admin@example.com',
password: 'helloworld',
password_confirmation: 'helloworld',
role: 'admin'
)
admin.skip_confirmation!
admin.save
# Create an Moderator user
moderator = User.new(
name: 'Moderator User',
email: 'moderator@example.com',
password: 'helloworld',
password_confirmation: 'helloworld',
role: 'moderator'
)
moderator.skip_confirmation!
moderator.save
# Create an Member user
member = User.new(
name: 'Member User',
email: 'member@example.com',
password: 'helloworld',
password_confirmation: 'helloworld'
)
member.skip_confirmation!
member.save
puts "Seed finished"
puts "#{User.count} users created"
puts "#{Post.count} posts created"
puts "#{Comment.count} comments created"
@JoseJRVazquez
Copy link
Author

require 'faker'

# Create Users
5.times do
  user = User.new(
    name:     Faker::Name.name,
    email:    Faker::Internet.email,
    password: Faker::Lorem.characters(10)
  )
  user.skip_confirmation!
  user.save
end
users = User.all


# Create Topics
15.times do
  Topic.create(
    name:         Faker::Lorem.sentence,
    description:  Faker::Lorem.paragraph 
  )         
end
topics = Topic.all


# Create Posts
50.times do
  Post.create(
    user:   users.sample,
    topic: topics.sample,
    title:  Faker::Lorem.sentence,
    body:   Faker::Lorem.paragraph
  )
end
posts = Post.all

# Create Comments
100.times do
  Comment.create(
    # user: users.sample,   # we have not yet associated Users with Comments
    post: posts.sample,
    body: Faker::Lorem.paragraph
  )
end

# Create an admin user
admin = User.new(
  name:     'Admin User',
  email:    'admin@example.com',
  password: 'helloworld',
  password_confirmation: 'helloworld',
  role:     'admin'
)
admin.skip_confirmation!
admin.save

# Create an Moderator user
moderator = User.new(
  name:     'Moderator User',
  email:    'moderator@example.com',
  password: 'helloworld',
  password_confirmation: 'helloworld',
  role:     'moderator'
)
moderator.skip_confirmation!
moderator.save

# Create an Member user
member = User.new(
  name:     'Member User',
  email:    'member@example.com',
  password: 'helloworld',
  password_confirmation: 'helloworld'
)
member.skip_confirmation!
member.save

puts "Seed finished"
puts "#{User.count} users created"
puts "#{Post.count} posts created"
puts "#{Comment.count} comments created"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment