Skip to content

Instantly share code, notes, and snippets.

@novohispano
Created November 5, 2013 18:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save novohispano/7323379 to your computer and use it in GitHub Desktop.
Save novohispano/7323379 to your computer and use it in GitHub Desktop.
Seeder example
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
@novohispano
Copy link
Author

For this to work, you need to have installed the faker gem (https://github.com/stympy/faker).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment