raking时Rails UnknownAttributeError

时间:2016-03-02 21:23:10

标签: ruby-on-rails ruby rubygems

在种子中,我创造了以下内容;

post = Post.create(
     title: Faker::Lorem.sentence(20),
     content: Faker::Lorem.paragraph,
     publish: true,
     administrator: administrator)

tag = Tag.create(name: Faker::Lorem.words)

post_tag = PostTag.create(post: post, tag: tag)

然后,在终端内我尝试“rake db:reset”并且它无法工作并显示以下错误;

-- initialize_schema_migrations_table()
   -> 0.0049s
 rake aborted!
 ActiveRecord::UnknownAttributeError: unknown attribute 'publish' for Post.
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-    4.2.5.1/lib/active_record/attribute_assignment.rb:59:in `rescue in  _assign_attribute'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord- 4.2.5.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:35:in `each'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/core.rb:566:in `init_attributes'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/core.rb:281:in `initialize'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/inheritance.rb:61:in `new'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/inheritance.rb:61:in `new'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/persistence.rb:33:in `create'
/Users/laurenwoodhams/Desktop/PROJECT/RAILS-BLOG/-t/db/seeds.rb:13:in `<top  (required)>'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/engine.rb:547:in `load_seed'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
 /Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:142:in `block (2 levels) in <top (required)>'
 NoMethodError: undefined method `publish=' for #<Post:0x007f98d0073d00>
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activemodel-4.2.5.1/lib/active_model/attribute_methods.rb:433:in `method_missing'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:54:in `public_send'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:35:in `each'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/core.rb:566:in `init_attributes'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/core.rb:281:in `initialize'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/inheritance.rb:61:in `new'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/inheritance.rb:61:in `new'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/persistence.rb:33:in `create'
/Users/laurenwoodhams/Desktop/PROJECT/RAILS-BLOG/-t/db/seeds.rb:13:in `<top (required)>'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/engine.rb:547:in `load_seed'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'
/Users/laurenwoodhams/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/railties/databases.rake:142:in `block (2 levels) in <top   (required)>'
Tasks: TOP => db:setup => db:seed
(See full trace by running task with --trace)

你能否告诉他为什么会推错这个错误?

谢谢

1 个答案:

答案 0 :(得分:0)

  

然后,在终端内我试图“rake db:reset”。 。

尝试跑步:

rake db:migrate
rake db:reset

您的Post对象根据您的数据库没有publish属性(无论出于何种原因)。

当您运行rake db:reset新迁移时,“迁移”。相反,它会加载架构(不包括未迁移的迁移)。

更多信息:https://stackoverflow.com/a/10302357/1026898