Skip to content

Instantly share code, notes, and snippets.

@xander-miller
Forked from darinthompson/seeds.rb
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xander-miller/8774802 to your computer and use it in GitHub Desktop.
Save xander-miller/8774802 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 # getting the first topic here
p = u.posts.create(
topic: topic,
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))
rand(3..7).times do
p.comments.create(
body: Faker::Lorem.paragraphs(rand(1..2)).join("\n"))
end
topics.rotate! # add this line to move the first topic to the last, so that posts get assigned to different topics.
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
puts "Seed finished"
puts "#{User.count} users created"
puts "#{Post.count} posts created"
puts "#{Comment.count} comments created"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment