首页 > 解决方案 > (ActiveModel::UnknownAttributeError) 在 heroku 中,但是在本地主机和 Heroku 本地工作

问题描述

我正在构建一个超级基本的格斗游戏词典。该应用程序可以在本地主机上运行,​​并且在 Heroku 本地上没有问题。

这是我的模型

class Vocabulary < ApplicationRecord
  validates :word, presence:true
  validates :definition, presence:true

  has_many :users, :through => :favorites
end

这是我的架构

create_table "vocabularies", force: :cascade do |t|
    t.string "word", null: false
    t.string "definition", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

这是我在运行 heroku run rails 控制台时收到的错误

/app/vendor/bundle/ruby/2.4.0/gems/activemodel-5.2.3/lib/active_model/attribute_assignment.rb:53:in `_assign_attribute': unknown attribute 'Word' for Vocabulary. (ActiveModel::UnknownAttributeError)

我试过:Heroku rake db:migrate,重置我的数据库,从 Heroku 删除应用程序并重建,哭了很多

我完全没有想法,所以任何帮助将不胜感激。

谢谢!

标签: ruby-on-railsheroku

解决方案


所以我的应用程序遇到了完全相同的问题。我在模型中添加了一个新字段,由于某种原因,在推动更改时,heroku 没有选择该字段并且显示未知属性错误。但是,当我检查 heroku 控制台时,我可以看到它确实在那里。尽管如此,这是我解决它的步骤。

  1. 首先重置heroku中的数据库heroku rake pg:reset(假设您使用的是pg)

  2. 然后,再次运行迁移... - heroku rake db:migrate (在运行此迁移时,它应该注销它实际上正在使用您要添加的字段运行该特定迁移)。

  3. 如果您有种子数据,那么... -heroku rake db:seed


推荐阅读