Created
November 5, 2013 18:06
-
-
Save novohispano/7323379 to your computer and use it in GitHub Desktop.
Seeder example
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
class Seeder | |
def create_data | |
100.times do | |
create_user | |
create_event | |
end | |
end | |
private | |
def create_user | |
first_name = Faker::Name.first_name | |
last_name = Faker::Name.last_name | |
user = User.create(name: "#{first_name} #{last_name}") | |
puts "Created user #{user.name}" | |
end | |
def create_event | |
event = Event.create( | |
type: events[Random.rand(0..3)], | |
created_at: Time.parse("2013-07-20 #{Random.rand(0..23)}:#{Random.rand(0..59)}:#{Random.rand(0..59)} UTC") | |
) | |
event.users << User.last | |
case event.type | |
when "high-five-another-user" | |
event.users << User.skip(1).last | |
when "comment" | |
comment = create_comment | |
event.comment = comment | |
puts "Created comment #{comment.message}" | |
end | |
puts "Created event #{event.id}" | |
end | |
def create_comment | |
Comment.create(message: Faker::Lorem.sentence) | |
end | |
def events | |
[ | |
"enter-the-room", | |
"leave-the-room", | |
"comment", | |
"high-five-another-user" | |
] | |
end | |
end | |
Seeder.new.create_data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For this to work, you need to have installed the faker gem (https://github.com/stympy/faker).