目前,要为Rails项目重建我的数据库,我在命令行中运行以下Rake任务:
bundle exec rake db:drop
bundle exec rake db:create
bundle exec rake db:migrate
bundle exec rake db:seed
我想把它们放在一个Rake任务中,所以我创建了一个lib/tasks/db.rake
文件:
namespace :db do
desc 'Drop and recreate the database(s)'
task :clean do
Rake::Task["db:drop"].invoke
Rake::Task["db:create"].invoke
Rake::Task["db:migrate"].invoke
Rake::Task["db:seed"].invoke # This doesn't work for some reason...
end
end
前几个任务运行正常,但db:seed
崩溃并出现一个奇怪的错误:
undefined method `username' for #<User:0xdfaa494>
[...]/gems/activemodel-3.2.12/lib/active_model/attribute_methods.rb:407:in `method_missing'
[...]/gems/activerecord-3.2.12/lib/active_record/attribute_methods.rb:149:in `method_missing'
[...]/lib/acts_as_followable/followable.rb:42:in `method_missing'
[...]/gems/friendly_id-4.0.9/lib/friendly_id/slugged.rb:253:in `should_generate_new_friendly_id?'
[...]/gems/friendly_id-4.0.9/lib/friendly_id/slugged.rb:273:in `set_slug'
[...]/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:407:in `_run__819679792__validation__197267883__callbacks'
[...]/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:405:in `__run_callback'
[...]/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
[...]/gems/activesupport-3.2.12/lib/active_support/callbacks.rb:81:in `run_callbacks'
[...]/gems/activemodel-3.2.12/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
[...]/gems/activemodel-3.2.12/lib/active_model/validations.rb:195:in `valid?'
[...]/gems/activerecord-3.2.12/lib/active_record/validations.rb:69:in `valid?'
[...]/gems/activerecord-3.2.12/lib/active_record/validations.rb:77:in `perform_validations'
[...]/gems/activerecord-3.2.12/lib/active_record/validations.rb:50:in `save'
[...]/gems/activerecord-3.2.12/lib/active_record/attribute_methods/dirty.rb:22:in `save'
[...]/gems/activerecord-3.2.12/lib/active_record/transactions.rb:259:in `block (2 levels) in save'
[...]/gems/activerecord-3.2.12/lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
[...]/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
[...]/gems/activerecord-3.2.12/lib/active_record/transactions.rb:208:in `transaction'
[...]/gems/activerecord-3.2.12/lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
[...]/gems/activerecord-3.2.12/lib/active_record/transactions.rb:259:in `block in save'
[...]/gems/activerecord-3.2.12/lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
[...]/gems/activerecord-3.2.12/lib/active_record/transactions.rb:258:in `save'
[...]/gems/activerecord-3.2.12/lib/active_record/relation/finder_methods.rb:294:in `find_or_instantiator_by_attributes'
[...]/gems/activerecord-3.2.12/lib/active_record/dynamic_matchers.rb:52:in `method_missing'
[...]/db/core_data/roles_and_users.rb:7:in `<top (required)>'
[...]/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `load'
[...]/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `block in load'
[...]/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
[...]/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `load'
[...]/db/seeds.rb:20:in `block in <top (required)>'
[...]/db/seeds.rb:19:in `each'
[...]/db/seeds.rb:19:in `<top (required)>'
[...]/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `load'
[...]/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `block in load'
[...]/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
[...]/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in `load'
[...]/gems/railties-3.2.12/lib/rails/engine.rb:520:in `load_seed'
[...]/gems/activerecord-3.2.12/lib/active_record/railties/databases.rake:333:in `block (2 levels) in <top (required)>'
[...]/lib/tasks/db.rake:11:in `block (2 levels) in <top (required)>'
[...]/bin/ruby_noexec_wrapper:14:in `eval'
[...]/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:seed
在命令行或运行应用程序时不会发生此错误。
更新:以下是有关代码库和架构的更多信息。
db/core_data/roles_and_users.rb
的第7行:
admin_user = User.find_or_create_by_email('valid@example.com') # not the actual email address used in the file
app/models/user.rb
的部分内容:
class User < ActiveRecord::Base
include ApplicationHelper, FriendlyId, ActsAsFollowable::Followable, Disableable
...
friendly_id :username, :use => :slugged
attr_accessible :username
...
username_format = /^[a-z\d]+([-_][a-z\d]+)*$/i
validates :username, :presence => true, :uniqueness => true, :format => { :with => username_format, :message => "can only contain letters, numbers, underscores (_) or dashes (-). Spaces are not allowed."}, :unreserved_word => true, :length => { :minimum => 3, :maximum => 60 }
...
end
username
是数据库中的VARCHAR(255)字段。
更新2:看起来Rake环境没有正确设置。如果我使用以下内容替换db/core_data/roles_and_users.rb
中的第7行:
admin_user = User.create(:email => 'valid@example.com', :username => 'administrator')
然后错误是:
unknown attribute: username
如果我将命令行(有效)的手动调用输出与Rake进行比较,并输出用户模型:
# Command line invocation -- WORKS
[2013-03-25 09:42:08] INFO Rails : Connecting to database specified by database.yml
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
Creating badges...
Creating Shipping Options...
For UPS...
For USPS...
Creating roles...
Creating users...
User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime, last_day_logged_in: datetime, consecutive_days_logged_in: integer, username: string, confirmation_sent_at: datetime, confirmed_at: datetime, confirmation_token: string, failed_attempts: integer, unlock_token: string, locked_at: datetime, super_user: boolean, facebook_id: string, facebook_email: string, fb_auth_token: string, slug: string, forem_admin: boolean, forem_state: string, forem_auto_subscribe: boolean, disabled: boolean, delta: boolean, latitude: float, longitude: float)
# Rake invocation -- DOESN'T WORK
** Invoke db:schema:load (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:load
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations
Creating badges...
Creating Shipping Options...
For UPS...
For USPS...
Creating roles...
Creating users...
User(id: integer, email: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, created_at: datetime, updated_at: datetime)
rake aborted!
undefined method `username' for #<User:0xdad7fc0>
正如您所看到的,Rake没有看到很多模型的字段,即使它们存在于数据库中。
答案 0 :(得分:1)
我不知道这是否有效,但请尝试:
namespace :db do
desc 'Drop and recreate the database(s)'
task :clean => :environment do
...
end
end
更新:如果这是您的任务的完整代码,您可以定义执行其他任务的任务,如下所示:
namespace :db do
desc 'Drop and recreate the database(s)'
task :clean => [:drop, :create, :migrate, :seed]
end
希望这有帮助! :)
更新2:我想我没有正确读取回溯...错误表明您的User对象没有定义username
方法。这可能是由两个不同的错误引起的:
您应该手动定义username
方法。
您在迁移中添加了username
字段,但不知何故它不存在。
检查迁移的拼写并确认您不需要实施选项1.然后检查您的db/core_data/roles_and_users.rb:7
文件(从回溯中提取)。
如果这不能解决您的问题,我不知道还能做什么:(
答案 1 :(得分:-1)
虽然这是一种迂回解决方案,但在db:clean
任务中,我会尝试使用system()
执行命令,或者在反引号中包含rake调用,即:
system("bundle exec rake db:drop")
system("bundle exec rake db:create")
system("bundle exec rake db:migrate")
system("bundle exec rake db:seed")
或
`bundle exec rake db:drop`
`bundle exec rake db:create`
`bundle exec rake db:migrate`
`bundle exec rake db:seed`
话虽如此,我还没有测试过,但它应该工作。