为什么' rake路线'命令还运行工厂内的rake db:seed命令?

时间:2016-08-27 08:18:56

标签: ruby-on-rails rake

我在seeds.rb文件中有一些种子数据。

我使用rake db:seed加载种子数据。它正确加载。

我在我的应用程序中输入一些新数据一切正常。

然后我运行' rake路线'检查路由的命令,我看到它运行rake db:seed命令,因为我可以看到seeds.rb文件的输出。

这是我的seeds.rb文件:

#Seeding the Role table
#
p "Removing existing #{Role.all.count} roles"
Role.destroy_all
p "Creating 3 roles"
[:proofreader, :admin, :super_admin].each do |role|
  Role.create( name: role )
end
p "Should have created 3 Roles, roles created: #{Role.all.count}"

#Seed the Employee table

#create super_admin employee
p "Removing existing #{Employee.all.count} employees"
Employee.destroy_all
p "Creating one employee"

super_admin = Employee.new(first_name: "Mitchell", last_name: "Gould", email: "mitchell@provenword.com", paypal_email: "go_mitchell@yayoo.ca", skype_id: "chellgouda", mobile: 66816927867, bachelor_degree: "Science", password: "chokta400",postal_code: "50100",address: "211/195 Soi 27, Sriwalee Klong Chun, T. Mae Hia, A. Muang", province_state: "Chiangmai", country: "Thailand", status: "active", os: "mac", role_ids: [Role.last.id])
super_admin.save!

p "Should have created #{Employee.all.count} employee with name #{Employee.first.first_name}."

这是我的工厂:

require 'faker'

Rails.application.load_seed


FactoryGirl.define do
  factory :employee do
    first_name { Faker::Name.first_name}
    last_name { Faker::Name.last_name}
    sequence(:email) { |n| "peterjohnson#{n}@example.com" }
    mobile 66816927867
    bio "MyText"
    address { Faker::Address.street_address}
    province_state { Faker::Address.state}
    country { Faker::Address.country}
    postal_code { Faker::Address.postcode}
    status :active
    bachelor_degree "B.Sc"
    password Faker::Internet.password(8)
    sequence(:paypal_email) { |n| "paypal_peterJohnson#{n}@example.com" }
    sequence(:skype_id) {|n| "peterjohnson_skype#{n}" }
    os :mac
    role_ids [Role.first.id]

    trait :proofreader do
        after(:create) {|employee| employee.add_role(:proofreader)}
    end

    trait :admin do
        after(:create) {|employee| employee.add_role(:admin)}
    end

    trait :super_admin do
        after(:create) {|employee| employee.add_role(:super_admin)}
    end
  end
end

以下是佣金路线的输出:

Running via Spring preloader in process 17957
"Removing existing 3 roles"
"Creating 3 roles"
"Should have created 3 Roles, roles created: 3"
"Removing existing 2 employees"
"Creating one employee"
"Should have created 1 employee with name Mitchell."
                      Prefix Verb   URI Pattern                            Controller#Action
        new_employee_session GET    /employees/sign_in(.:format)           devise/sessions#new
            employee_session POST   /employees/sign_in(.:format)           devise/sessions#create
    destroy_employee_session GET    /employees/sign_out(.:format)          devise/sessions#destroy
           employee_password POST   /employees/password(.:format)          devise/passwords#create
       new_employee_password GET    /employees/password/new(.:format)      devise/passwords#new
      edit_employee_password GET    /employees/password/edit(.:format)     devise/passwords#edit
                             PATCH  /employees/password(.:format)          devise/passwords#update
                             PUT    /employees/password(.:format)          devise/passwords#update
cancel_employee_registration GET    /employees/cancel(.:format)            employees/registrations#cancel
       employee_registration POST   /employees(.:format)                   employees/registrations#create
   new_employee_registration GET    /employees/sign_up(.:format)           employees/registrations#new
  edit_employee_registration GET    /employees/edit(.:format)              employees/registrations#edit
                             PATCH  /employees(.:format)                   employees/registrations#update
                             PUT    /employees(.:format)                   employees/registrations#update
                             DELETE /employees(.:format)                   employees/registrations#destroy
          new_client_session GET    /clients/sign_in(.:format)             devise/sessions#new
              client_session POST   /clients/sign_in(.:format)             devise/sessions#create
      destroy_client_session GET    /clients/sign_out(.:format)            devise/sessions#destroy
             client_password POST   /clients/password(.:format)            devise/passwords#create
         new_client_password GET    /clients/password/new(.:format)        devise/passwords#new
        edit_client_password GET    /clients/password/edit(.:format)       devise/passwords#edit
                             PATCH  /clients/password(.:format)            devise/passwords#update
                             PUT    /clients/password(.:format)            devise/passwords#update
  cancel_client_registration GET    /clients/cancel(.:format)              devise/registrations#cancel
         client_registration POST   /clients(.:format)                     devise/registrations#create
     new_client_registration GET    /clients/sign_up(.:format)             devise/registrations#new
    edit_client_registration GET    /clients/edit(.:format)                devise/registrations#edit
                             PATCH  /clients(.:format)                     devise/registrations#update
                             PUT    /clients(.:format)                     devise/registrations#update
                             DELETE /clients(.:format)                     devise/registrations#destroy
          quotation_requests GET    /quotation_requests(.:format)          quotation_requests#index
                             POST   /quotation_requests(.:format)          quotation_requests#create
       new_quotation_request GET    /quotation_requests/new(.:format)      quotation_requests#new
      edit_quotation_request GET    /quotation_requests/:id/edit(.:format) quotation_requests#edit
           quotation_request GET    /quotation_requests/:id(.:format)      quotation_requests#show
                             PATCH  /quotation_requests/:id(.:format)      quotation_requests#update
                             PUT    /quotation_requests/:id(.:format)      quotation_requests#update
                             DELETE /quotation_requests/:id(.:format)      quotation_requests#destroy
              show_dashboard GET    /dashboard(.:format)                   dashboard#show
                        root GET    /

当我运行rake路由时,如何阻止Rails在工厂中重新运行种子数据?

3 个答案:

答案 0 :(得分:0)

这可能是Spring的一个问题。尝试停止Spring并重新运行rake路由任务。

bundle exec spring stop
bundle exec rake routes

答案 1 :(得分:0)

我不知道这是否是完美的解决方案,但它对我有用。

我刚刚添加了以下内容,只在测试模式下运行命令

Rails.application.load_seed if  Rails.env.test?

答案 2 :(得分:0)

事实证明,我的gem文件中的开发组中有FactoryGirl。一旦我将它移动到仅测试组,当我执行rake db:seed

时,没有加载seeds.rb文件