Created
April 9, 2014 23:55
-
-
Save anonymous/10331344 to your computer and use it in GitHub Desktop.
db seeds
This file contains 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
# This file should contain all the record creation needed to seed the database with its default values. | |
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). | |
# | |
# Examples: | |
# | |
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) | |
# Mayor.create(name: 'Emanuel', city: cities.first) | |
require 'faker' | |
#create 15 topics | |
topics = [ ] | |
15.times do | |
topics << Topic.create( | |
name: Faker::Lorem.sentence, | |
description: Faker::Lorem.paragraph) | |
end | |
5.times do | |
topic = topics.first | |
post = Post.create( | |
user: user, | |
topic: topic, | |
title: Faker::Lorem.sentence, | |
body: Faker::Lorem.paragraph) | |
post.update_attribute(:created_at, Time.now - rand(600..31536000)) | |
topics.rotate! | |
end | |
admin = User.new( | |
name: 'Admin User', | |
email: '[email protected]', | |
password: 'helloworld', | |
password_confirmation: 'helloworld') | |
admin.skip_confirmation! | |
admin.save | |
admin.update_attribute(:role, 'admin') | |
moderator = User.new( | |
name: 'Moderator User', | |
email: '[email protected]', | |
password: 'helloworld', | |
password_confirmation: 'helloworld') | |
moderator.skip_confirmation! | |
moderator.save | |
moderator.update_attribute(:role, 'moderator') | |
member = User.new( | |
name: 'Member User', | |
email: '[email protected]', | |
password: 'helloworld', | |
password_confirmation: 'helloworld') | |
member.skip_confirmation! | |
member.save | |
######################################################## | |
5.times do | |
password = Faker::Lorem.characters(10) | |
user = User.new( | |
name: Faker::Name.name, | |
email: Faker::Internet.email, | |
password: password, | |
password_confirmation: password) | |
user.skip_confirmation! | |
user.save | |
5.times do | |
post = Post.create( | |
user: user, | |
title: Faker::Lorem.sentence, | |
body: Faker::Lorem.paragraph) | |
# set the created_at to a time within the past year | |
post.update_attribute(:created_at, Time.now - rand(600..31536000)) | |
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