Skip to content

Instantly share code, notes, and snippets.

@axelkanakan
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save axelkanakan/9609437 to your computer and use it in GitHub Desktop.
Save axelkanakan/9609437 to your computer and use it in GitHub Desktop.
require 'faker'
topics = []
15.times do
topics << Topic.create(
name: Faker::Lorem.words(rand(1..10)).join(" "),
description: Faker::Lorem.paragraph(rand(1..4))
)
end
rand(4..10).times do
password = Faker::Lorem.characters(10)
u = User.new(
name: Faker::Name.name,
email: Faker::Internet.email,
password: password,
password_confirmation: password)
u.skip_confirmation!
u.save
# Note: by calling `User.new` instead of `create`,
# we create an instance of a user which isn't saved to the database.
# The `skip_confirmation!` method sets the confirmation date
# to avoid sending an email. The `save` method updates the database.
rand(5..12).times do
topic = topics.first
p = u.posts.create(
title: Faker::Lorem.words(rand(1..10)).join(" "),
body: Faker::Lorem.paragraphs(rand(1..4)).join("\n"))
# set the created_at to a time within the past year
p.update_attribute(:created_at, Time.now - rand(600..31536000))
topics.rotate!
rand(3..7).times do
p.comments.create(
body: Faker::Lorem.paragraphs(rand(1..2)).join("\n"))
end
end
end
u = User.new(
name: 'Admin User',
email: 'admin@example.com',
password: 'helloworld',
password_confirmation: 'helloworld')
u.skip_confirmation!
u.save
u.update_attribute(:role, 'admin')
u = User.new(
name: 'Moderator User',
email: 'moderator@example.com',
password: 'helloworld',
password_confirmation: 'helloworld')
u.skip_confirmation!
u.save
u.update_attribute(:role, 'moderator')
u = User.new(
name: 'Member User',
email: 'member@example.com',
password: 'helloworld',
password_confirmation: 'helloworld')
u.skip_confirmation!
u.save
post_array = Post.all
User.all.each do |user|
random_post = post_array[rand(1..post_array.length)]
rand(3..7).times do
comment = user.comments.new(body: Faker::Lorem.paragraphs(rand(1..2)).join("\n"))
comment.post = random_post
comment.save
end
end
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