2013-02-15 14 views
0

我检查了那些已经被问过这个问题的问题,‘有很多’,但我没有找到一个解决方案。法克尔“不知道如何建立任务?

我有一个相当大任务,文件名是 'sample_employee_data.rake' ......所以这里有云:

 namespace :db do 
     desc "Fill database with sample Employee data" 
     task populate: :environment do 

      @gender = ["Male", "Female"] 
      @role = ["Staff", "Support", "Team_Leader", "Manager", "Director"] 
      @marital_status = ["Single", "Married"] 
      @primary_position = ["Household", "Job Developer", "Job Coach", 
        "Job Support", "Personal Care"] 
      @trained_position = ["Household", "Job Developer", "Job Coach", 
        "Job Support", "Personal Care"] 
      @emer_contact_relationship = ["Friend", "Brother", "Sister", "Aunt", 
           "Uncle", "Cousin", "Nephew", "Father", 
           "Mother", "Spouse"] 

      def randomDate(params={}) 
      years_back = params[:year_range] || 5 
      latest_year = params[:year_latest] || 0 
      year = (rand * (years_back)).ceil + 
      (Time.now.year - latest_year - years_back) 
      month = (rand * 12).ceil 
      day = (rand * 31).ceil 
      series = [date = Time.local(year, month, day)] 
      if params[:series] 
       params[:series].each do |some_time_after| 
       series << series.last + (rand * some_time_after).ceil 
       end 
       return series 
       end 
       date 
      end 

      Employee.create!(first_name:  "Shelly", 
       last_name:     "Houghton", 
       mi:       "M", 
       full_name:     "Shelly M Houghton", 
       marital_status:    "Single",     
       gender:      "Female", 
       hire_date:     "2000-04-16", 
       primary_position:   "Manager", 
       trained_position:   "Job Developer", 
       email:      "[email protected]", 
       active:      true, 
       address1:     "76th Ave", 
       city:      "Frave", 
       zip_code:     "54806", 
       state:      "WI", 
       emp_home_ph:     "1-111-111-1111", 
       emp_mobile_ph:    "1-222-222-2222", 
       emer_contact_first_name:  "Kenneth", 
       emer_contact_last_name:  "Koening", 
       emer_contact_relationship: "Friend", 
       emer_contact_ph:    "1-333-333-3333", 
       role:      "Manager", 
       birth_date:     "1982-08-21", 
       admin:      true, 
       password:     "90nothguoh", 
       password_confirmation:  "90nothguoh") 

     99.times do |n| 
      first_name = Faker::Name.first_name 
      last_name = Faker::Name.last_name 
      mi = ("A".."Z").to_a[rand(26)] 
      full_name = Faker::Name.full_name 
      marital_status = @marital_status[rand(2)].to_s 
      gender = @gender[rand(2)].to_s 
      hire_date = randomDate(:year_range => 60, :year_latest => 12) 
      birth_date = randomDate(:year_range => 60, :year_latest => 22) 
      primary_position = @primary_position[rand(5)].to_s 
      trained_position = @trained_position[rand(5)].to_s 
      email = "emp-#{n+1}@example.org" 
      active = [true, false][rand(2)] 
      admin = (1 == rand(2) ? true : false) 
      role = @role[rand(5)].to_s 
      address1 = "Seaview-#{n+5}Way" 
      city = Faker::Lorem.words(1).to_s.capitalize 
      state = Faker::Address.us_state() 
      zip_code = Faker::Address.zip_code 
      emp_home_ph = Faker::PhoneNumber.phone_number 
      emp_mobile_ph = Faker::PhoneNumber.phone_number 
      emer_contact_first_name = Faker::Name.first_name 
      emer_contact_last_name = Faker::Name.last_name 
      emer_contact_relationship = @emer_contact_relationship[rand(10)].to_s 
      emer_contact_ph = Faker::PhoneNumber.phone_number 
      password = "uniqueone" 
      Employee.create!(first_name: first_name, mi: mi, last_name: last_name, 
        full_name: full_name, marital_status: marital_status, 
        gender: gender, birth_date: birth_date, hire_date: hire_date, 
        primary_position: primary_position, trained_position: 
        trained_position, email: email, role: role, address1: 
        address1, city: city, state: state, zip_code: zip_code, 
        emp_home_ph: emp_home_ph, emp_mobile_ph: emp_mobile_ph, 
        emer_contact_first_name: emer_contact_first_name, 
        emer_contact_last_name: emer_contact_last_name, 
        emer_contact_relationship: emer_contact_relationship, 
        emer_contact_ph: emer_contact_ph, password: password, 
        password_confirmation: password) 
     end 
     end 
    end 

我跑:

 rake sample_employee_data.rake 

,我得到这个标志:

rake aborted! 
    Don't know how to build task 'sample_employee_data.rake' 
    home/me/.rvm/gems/[email protected]/bin/ruby_noexec_wrapper:14:in 'eval' 
    home/me/.rvm/gems/[email protected]/bin/ruby_noexec_wrapper:14:in '<main>' 

任何人都可以发现问题......我很抱歉如此长的文件。

谢谢。

+0

仅供参考,获得种子数据到数据库的标准的Rails-Y的方法是使用'db:seed'任务;看看[这个问题和答案](http://stackoverflow.com/questions/761123/what-is-the-best-way-to-seed-a-database-in-rails)。 – nickgrim 2013-02-18 10:20:48

+0

谢谢@nickgrim ...我会查看栏目'种子数据' – thomasvermaak 2013-02-18 17:37:27

回答

3

这里有一些事情正在进行。

当您运行$ rake sample_employee_data.rake你问耙耙文件 - 但耙响应任务:

$ rake some_namespace:some_task_in_that_namespace

您还使用自定义.rake file而不是传统的Rakefile - 这是罚款,但这意味着您需要明确告诉rake您没有使用其默认的Rakefile

$ rake --rakefile your_custom_rake_file.rake some_task

所以你的情况,你需要像这样发出rake命令:

$ rake --rakefile sample_employee_data.rake db:populate

+0

感谢@Rick Winfrey为您的答案...这是一个非常全面的人,我非常感谢您的时间,并认为您已将其放入您的答案。我按照你的建议运行了这个命令,我仍然耙得中止!旗。找不到Rakefile(查找:sample_employee_data.rake)...我必须给它rakefile的路径吗? ... 再次感谢。 – thomasvermaak 2013-02-17 19:07:06

+1

您是否在rake文件所在的目录中运行rake命令?如果你在同一个目录的时候,你可以只问题: '$耙--rakefile sample_employee_data.rake DB:populate' 但如果没有,你需要的路径提供给文件: ' $ rake --rakefile path/to/sample_employee_data.rake db:populate' 让我知道结果如何。 – 2013-02-17 19:48:10

+0

好的......我将目录改为lib/tasks /并按照您的建议运行该文件,现在该标志是“不知道如何构建任务”环境“...在文件中我有任务”任务填充::环境做“......它对环境部分有问题。 – thomasvermaak 2013-02-17 20:38:00

相关问题