Created
          May 23, 2012 14:24 
        
      - 
      
- 
        Save enriclluelles/2775500 to your computer and use it in GitHub Desktop. 
    Multithreaded seeds
  
        
  
    
      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
    
  
  
    
  | Rails.configuration.cache_classes = true | |
| Rails.configuration.threadsafe! | |
| Rails.configuration.allow_concurrency = true | |
| require "#{Rails.root}/test/factories" | |
| Dir.glob(File.join(Rails.root, 'app', '{lib,app}', '*.rb')).each{|f| require f} | |
| Rails.application.eager_load! | |
| puts "Creating users..." | |
| users = [] | |
| users << User.create(:email=>"[email protected]", :name=>"Francesc", :surnames=>"Masana", :password=>"zlworks", :password_confirmation=>"zlworks", :gender=>"male") | |
| users << User.create(:email=>"[email protected]", :name=>"Sergi", :surnames=>"Villanueva", :password=>"zlworks", :password_confirmation=>"zlworks", :gender=>"male") | |
| users << User.create(:email=>"[email protected]", :name=>"Mireia", :surnames=>"Garcia", :password=>"zlworks", :password_confirmation=>"zlworks", :gender=>"female") | |
| 10.times{ users << Factory.create(:user) } | |
| puts "Users created" | |
| puts "Creating locations..." | |
| CreateTowns.create_all | |
| puts "Locations created" | |
| puts "Creating accommodations..." | |
| towns = Town.all | |
| pictures = Dir.glob(File.join(Rails.root, 'db', 'seeds', 'pictures', '*.jpg')) | |
| threads = [] | |
| 4.times do |j| | |
| threads << Thread.new do | |
| 25.times do |i| | |
| a = Factory.create(:accommodation, town: towns.sample, user: users.sample) | |
| ap = AccommodationPicture.new(:accommodation => a, :info => Faker::Lorem.words(1), :priority => 1) | |
| ap.image = pictures[(i * j) % pictures.size] | |
| ap.save | |
| end | |
| ActiveRecord::Base.connection_pool.clear_stale_cached_connections! | |
| end | |
| end | |
| threads.each{|t| t.join} | |
| puts "Accommodations created..." | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment