【问题标题】:Faker "Don't Know How to Build Task?Faker“不知道如何构建任务?
【发布时间】:2013-02-01 00:16:21
【问题描述】:

我查看了关于这个主题已经提出的问题,“有很多”,但我没有找到解决方案。

我有一个相当大的任务,文件名是“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:                       "shoughton@example.com",
                 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/ruby-1.9.3-p385@rails3212/bin/ruby_noexec_wrapper:14:in 'eval'
  home/me/.rvm/gems/ruby-1.9.3-p385@rails3212/bin/ruby_noexec_wrapper:14:in '<main>'

任何人都可以发现问题...对于这么长的文件,我深表歉意。

谢谢。

【问题讨论】:

  • 仅供参考,将种子数据放入数据库的标准 Rails-y 方法是使用 db:seed 任务;看看this question and answer
  • 谢谢@nickgrim ...我会查看 railcast 'seed-data'

标签: rake-task faker


【解决方案1】:

这里发生了几件事。

当您运行 $ rake sample_employee_data.rake 时,您要求 rake 对您的文件进行 rake - 但 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

【讨论】:

  • 感谢@Rick Winfrey 的回答......他真的很全面,我非常感谢您花时间和思考您的回答。我按照您的建议运行了命令,但仍然中止了耙子!旗帜。没有找到 Rakefile(正在寻找:sample_employee_data.rake)...我必须给它提供 rakefile 的路径吗? ...再次感谢。
  • 您是否在与 rake 文件相同的目录中运行 rake 命令?如果您在同一目录中,则可以发出:$ rake --rakefile sample_employee_data.rake db:populate 但如果不是,则需要提供文件的路径:$ rake --rakefile path/to/sample_employee_data.rake db:populate 让我知道结果如何。
  • 好的...我将目录更改为 lib/tasks/ 并按照您的建议运行文件,现在标志是“不知道如何构建任务'环境'”...在文件中我有“task populate: :environment do”...环境部分有问题。
  • 我删除了“环境”,它现在可以运行了,但是有这个问题; “我很抱歉打扰了”,问题在于语法错误...... 99.times do |n| ...“意外的关键字 do_block”期待“结束”。
  • 嗯,它不是很有效,但它是最好的答案,所以我会留下它......谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-01-20
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多