Created
February 2, 2014 20:50
-
-
Save darinthompson/8774735 to your computer and use it in GitHub Desktop.
My seeds.rb file...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'faker' | |
topics = [] | |
15.times do | |
topics << Topic.create( | |
name: Faker::Lorem.words(rand(1..10)).join(" "), | |
description: Faker::Lorem.paragraph(rand(1..4)) | |
) | |
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)) | |
topics.rotate! # add this line to move the first topic to the last, so that posts get assigned to different topics. | |
# comments... | |
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 | |
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)) | |
rand(3..7).times do | |
p.comments.create( | |
body: Faker::Lorem.paragraphs(rand(1..2)).join("\n")) | |
end | |
end | |
u = User.new( | |
name: 'Admin User', | |
email: '[email protected]', | |
password: 'helloworld', | |
password_confirmation: 'helloworld') | |
u.skip_confirmation! | |
u.save | |
u.update_attribute(:role, 'admin') | |
u = User.new( | |
name: 'Moderator User', | |
email: '[email protected]', | |
password: 'helloworld', | |
password_confirmation: 'helloworld') | |
u.skip_confirmation! | |
u.save | |
u.update_attribute(:role, 'moderator') | |
u = User.new( | |
name: 'Member User', | |
email: '[email protected]', | |
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 | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment