daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Jul 11th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Rake task emulating Rails 3 db:seed
  2. namespace :db do
  3.   desc <<-EOS
  4.     Loads seed data from <RAILS_ROOT>/db/seeds2.rb.
  5.   EOS
  6.  
  7.   task :seed2 => :environment do
  8.     seed_file = File.join(RAILS_ROOT, "db", "seeds2.rb")
  9.  
  10.     load(seed_file) if File.exists?(seed_file)
  11.   end
  12. end
  13.  
  14. ## seeds2.rb
  15. # A pseudo-backport from Rails 3 seeds.
  16. # In general, use "seeds2" instead of "seeds" for commands.
  17.  
  18. # This file should contain all the record creation needed to seed the database with its default values.
  19. # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
  20. #
  21. # Examples:
  22. #
  23. # cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
  24. # Major.create(:name => 'Daley', :city => cities.first)
  25.  
  26. SEED_DIR = File.join(RAILS_ROOT, "db", "seeds")
  27.  
  28. class CreateSeedData < ActiveRecord::Migration
  29.   def self.up
  30.     unless (table_exists?("seed_datas"))
  31.       create_table :seed_datas do |t|
  32.         t.string :name, :null => false
  33.         t.timestamps
  34.       end
  35.       add_index("seed_datas", :name, :unique => true)
  36.     end
  37.   end
  38. end
  39.  
  40. class SeedData < ActiveRecord::Base
  41. end
  42.  
  43. def load_seed(seed_index)
  44.   file = seed_index + ".rb"
  45.   source = File.join(SEED_DIR, file)
  46.  
  47.   seed = SeedData.find_by_name(seed_index)
  48.   if (seed.nil?)
  49.     puts "- Loading data from #{source}"
  50.     success = load source
  51.     if (success)
  52.       SeedData.create!(:name => seed_index)
  53.     else
  54.       puts "! An error occurred while processing #{source}"
  55.     end
  56.   end
  57. end
  58.  
  59. seeds = [
  60.     "seed_file_1",
  61.     "seed_file_2",
  62. ]
  63.  
  64. CreateSeedData.up
  65. seeds.each {|s| load_seed s}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top