【问题标题】:Trying to seed database, getting error: unable to autoload constant尝试播种数据库,出现错误:无法自动加载常量
【发布时间】:2014-03-07 03:08:56
【问题描述】:

我在运行“sudo rake db:setup”时遇到以下错误。 Seeds.rb 文件和模型包括:

boris@alpha:~/Desktop/Wholetail$ sudo rake db:drop:all
boris@alpha:~/Desktop/Wholetail$ sudo rake --trace db:setup
** Invoke db:setup (first_time)
** Invoke db:schema:load_if_ruby (first_time)
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:create
** Invoke environment (first_time)
** Execute environment
** Execute db:schema:load_if_ruby
** Invoke db:schema:load (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:schema:load
-- enable_extension("plpgsql")
   -> 0.4503s
-- create_table("customers", {:force=>true})
   -> 0.1893s
-- create_table("deal_categories", {:force=>true})
   -> 0.1218s
-- create_table("deals", {:force=>true})
   -> 0.1672s
-- create_table("merchant_categories", {:force=>true})
   -> 0.1104s
-- create_table("merchants", {:force=>true})
   -> 0.1671s
-- initialize_schema_migrations_table()
   -> 0.1442s
** Invoke db:structure:load_if_sql (first_time)
** Invoke db:create 
** Invoke environment 
** Execute db:structure:load_if_sql
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment 
** Execute db:abort_if_pending_migrations
rake aborted!
Unable to autoload constant Deal_category, expected /home/boris/Desktop/Wholetail/app/models/deal_category.rb to define it
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:464:in `load_missing_constant'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:184:in `const_missing'
/home/boris/Desktop/Wholetail/db/seeds.rb:8:in `block in <top (required)>'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/core_ext/range/each.rb:8:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/core_ext/range/each.rb:8:in `each_with_time_with_zone'
/home/boris/Desktop/Wholetail/db/seeds.rb:7:in `<top (required)>'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `block in load'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
/usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
/usr/local/lib/ruby/gems/2.0.0/gems/railties-4.0.2/lib/rails/engine.rb:540:in `load_seed'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/tasks/database_tasks.rb:154:in `load_seed'
/usr/local/lib/ruby/gems/2.0.0/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:181:in `block (2 levels) in <top (required)>'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `call'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `block in execute'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `execute'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:197:in `block in invoke_prerequisites'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:195:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:195:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:174:in `block in invoke_with_call_chain'
/usr/local/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_with_call_chain'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:161:in `invoke'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:149:in `invoke_task'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `each'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `block in top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:115:in `run_with_threads'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:100:in `top_level'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:78:in `block in run'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:165:in `standard_exception_handling'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:75:in `run'
/usr/local/lib/ruby/gems/2.0.0/gems/rake-10.1.1/bin/rake:33:in `<top (required)>'
/usr/local/bin/rake:23:in `load'
/usr/local/bin/rake:23:in `<main>'
Tasks: TOP => db:setup => db:seed

boris@alpha:~/Desktop/Wholetail$

seeds.rb 文件:

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).


# Create deal categories
# Name
for i in 0..9
        deal_categories=Deal_category.create([
            {deal_category_name: Faker::Commerce.department}
        ])
end

# Create Deals
# Product, Title, Description, Price
for i in 0..99

        category=deal_category.first(offset: rand(deal_category.count))

        deals=Deal.create([
            {deal_product: Faker::Commerce.product},
            {deal_title:  Faker::Company.catch_phrase},
            {deal_description: Faker::Lorem.paragraph},
            {deal_price: 100},
            {deal_category: category}
            ])
end


# create merchant categories
# name
for i in 0..9
        merchant_categories=Merchant_category.create([
            {merchant_category_name: Faker::Commerce.department}
        ])
end

# Create merchants
# Name, email, phone, address, url
for i in 0..99
        category=merchant_category.first(offset: rand(merchant_category.count))

        merchants=Merchant.create([
            {merchant_name: Faker::Company.name},
            {merchant_category: category},
            {merchant_email: Faker::Internet.name},
            {merchant_phone: Faker::PhoneNumber.phone_number},
            {merchant_address: Faker::Address.street_address},
            {merchant_url: merchant_name + ".com"}
            ])
end


# Create customers
# Name, phone number, email, street address
for i in 0..99

        deal_history1=deal.first(offset: rand(deal.count))
        deal_history2=deal.first(offset: rand(deal.count))
        deal_history3=deal.first(offset: rand(deal.count))
        DEAL_HISTORY = [deal_history1, deal_history2, deal_history3]

        customers=Customer.create([
            {customer_name: Faker::Name.name},
            {customer_phone: Faker::PhoneNumber.phone_number},
            {customer_email: Faker::Internet.email},
            {customer_address: Faker::Address.street_address},
            {customer_deal_history: DEAL_HISTORY}
        ])

结束

Deal_categories 模型:

    class DealCategory < ActiveRecord::Base

    has_many :deals

    validates :deal_category_name, presence:true, uniqueness: true
end

非常感谢任何帮助。 谢谢。

@first_commenter: 即使在我迁移所有内容之后,我也得到了这个:

boris@alpha:~/Desktop/Wholetail$ sudo rake db:setup
wholetail_development already exists
wholetail_test already exists
-- enable_extension("plpgsql")
   -> 0.3085s
-- create_table("customers", {:force=>true})
   -> 0.3291s
-- create_table("deal_categories", {:force=>true})
   -> 0.1332s
-- create_table("deals", {:force=>true})
   -> 0.1882s
-- create_table("merchant_categories", {:force=>true})
   -> 0.1330s
-- create_table("merchants", {:force=>true})
   -> 0.2007s
-- initialize_schema_migrations_table()
   -> 0.0082s
rake aborted!
Unable to autoload constant Deal_category, expected /home/boris/Desktop/Wholetail/app/models/deal_category.rb to define it
/home/boris/Desktop/Wholetail/db/seeds.rb:8:in `block in <top (required)>'
/home/boris/Desktop/Wholetail/db/seeds.rb:7:in `<top (required)>'
Tasks: TOP => db:setup => db:seed
(See full trace by running task with --trace)

【问题讨论】:

    标签: ruby-on-rails autoload seeding


    【解决方案1】:

    您在种子.rb 文件中引用了一个Deal_category 类,但该类的名称是DealCategory

    更改以下行

    deal_categories=Deal_category.create([
                {deal_category_name: Faker::Commerce.department}
            ])
    

    deal_categories=DealCategory.create([
                {deal_category_name: Faker::Commerce.department}
            ])
    

    【讨论】:

      【解决方案2】:

      从错误输出中我看到它中止了,因为存在挂起的迁移,您可以通过以下方式解决它:rake db:migrate

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-12
        • 1970-01-01
        相关资源
        最近更新 更多