目前,要为 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
此错误不会发生在命令行上,也不会在运行应用程序时发生。
更新:这是有关代码库和架构的更多信息。
第 7 行db/core_data/roles_and_users.rb
:
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 环境设置不正确。如果我将第 7 行替换db/core_data/roles_and_users.rb
为以下内容:
admin_user = User.create(:email => 'valid@example.com', :username => 'administrator')
那么错误是:
unknown attribute: username
如果我将命令行(有效)手动调用的输出与 Rake 进行比较,我还会输出 User 模型:
# 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 并没有看到很多模型的字段,即使它们存在于数据库中。