【问题标题】:Why is the 'rake routes' command also running the rake db:seed command that is inside a factory?为什么“rake routes”命令还运行工厂内的 rake db:seed 命令?
【发布时间】:2017-01-03 21:00:52
【问题描述】:

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

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

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

然后我运行“rake routes”命令来检查路由,我看到它运行了 rake db:seed 命令,因为我可以看到 seed.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

这是 rake 路由的输出:

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 在工厂中重新运行种子数据?

【问题讨论】:

    标签: ruby-on-rails rake


    【解决方案1】:

    这可能是 Spring 的问题。尝试停止 Spring 并重新运行您的 rake routes 任务。

    bundle exec spring stop
    bundle exec rake routes
    

    【讨论】:

    • 我停止了 spring 并重新运行了 rake 路线。仍在运行 seed.rb。
    • 我刚刚发现我的一个工厂中有这样一行:Rails.application.load_seed。当我删除它并运行 rake 路由时,它不会运行 seed.rb 文件。所以这个问题已经更新,以找出为什么当我做 rake 路线时,工厂中的这个命令会运行。感谢您的帮助。
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-02
        • 1970-01-01
        • 2014-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多