简体   繁体   中英

Why is the 'rake routes' command also running the rake db:seed command that is inside a factory?

I have some seed data in the seeds.rb file.

I load the seed data using rake db:seed. It load correctly.

I enter some new data into my application all works fine.

Then I run the 'rake routes' command to check out the routes and I see that it runs the rake db:seed command because I can see the output from the seeds.rb file.

Here is my seeds.rb file:

#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}."

Here is my Factory:

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

Here is the output from rake routes:

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    /

How can I stop Rails from re-running the seed data in the factory when I run rake routes?

This might be an issue with Spring. Try stopping Spring and re-running your rake routes task.

bundle exec spring stop
bundle exec rake routes

I don't know if this is the perfect solution but it worked for me.

I just added the following to only run the command in test mode

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

It turns out that I has FactoryGirl in my development group in my gem file. Once I moved it to the test only group the seeds.rb file was not loaded when I performed rake db:seed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM