Skip to content

Instantly share code, notes, and snippets.

@trevorturk
Created September 16, 2008 21:26
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 trevorturk/11137 to your computer and use it in GitHub Desktop.
Save trevorturk/11137 to your computer and use it in GitHub Desktop.
# http://railscasts.com/episodes/126-populating-a-database
# http://github.com/ryanb/populator/tree/master
namespace :db do
task :populate => :environment do
require 'populator' # http://populator.rubyforge.org/
require 'faker' # http://faker.rubyforge.org/rdoc/
[User, Event, Link].each(&:delete_all)
def random(model)
ids = ActiveRecord::Base.connection.select_all("SELECT id FROM #{model.to_s.pluralize.downcase}")
model.find(ids[rand(ids.length)]["id"].to_i) unless ids.blank?
end
User.populate 5 do |r|
r.login = Faker::Internet.user_name
r.email = Faker::Internet.email
end
Event.populate 20 do |r|
r.user_id = random(User).id
r.title = Faker::Lorem.sentence
r.start_date = 2.months.ago..Time.now
r.date_granularity = 1
r.location = ['Chicago, IL', 'San Francisco, CA', 'Berlin, Germany', 'Paris, France']
end
LINKS = %w(
http://google.com
http://yahoo.com
http://vimeo.com/1185346
http://flickr.com/photos/almosteffortless/2669660449/
http://farm4.static.flickr.com/3230/2670484476_0e2e25cf68.jpg?v=0
http://farm4.static.flickr.com/3175/2669669461_af57049651_b.jpg
http://www.youtube.com/watch?v=7-6KJxP5bbY
http://www.youtube.com/watch?v=7-NOZU2iPA8
http://www.youtube.com/watch?v=muLIPWjks_M
)
Link.populate 100 do |r|
r.user_id = random(User).id
r.event_id = random(Event).id
r.url = LINKS
end
Link.all.each do |r|
r.parse_url
r.save(false)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment