2016-08-27 71 views
0

我在seeds.rb文件中有一些種子數據。爲什麼'rake routes'命令也運行工廠內的rake db:seed命令?

我使用rake db:seed加載種子數據。它正確加載。

我在我的應用程序中輸入一些新的數據都很好。

然後,我運行'rake routes'命令檢查路由,並看到它運行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: "[email protected]", paypal_email: "[email protected]", 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 /

如何停止當我運行耙路線時,Rails在工廠重新運行種子數據?

回答

0

運行命令事實證明,我有FactoryGirl在我的寶石文件我的開發團隊。一旦我將它移動到只測試組,seeds.rb文件在我執行rake db時未加載:種子

0

這可能是Spring的一個問題。嘗試停止Spring並重新運行你的耙路線任務。

bundle exec spring stop 
bundle exec rake routes 
+0

我停止了春天並重新運行了耙路徑。仍然有種子.rb運行。 – chell

+0

我剛剛發現我有這樣一行:Rails.application.load_seed在我的一個工廠裏面。當我刪除它並運行rake路由時,它不會運行seeds.rb文件。所以這個問題已經得到更新,以瞭解爲什麼當我執行耙路時,工廠中的這個命令會運行。謝謝你的幫助。 – chell

0

我不知道這是否是完美的解決方案,但它爲我工作。

我只是添加了以下僅在測試模式

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