【问题标题】:Undefined method add_column when booting rails server in Heroku在 Heroku 中启动 rails 服务器时未定义的方法 add_column
【发布时间】:2014-08-03 22:12:50
【问题描述】:

当我尝试在 heroku 中运行 rails 时,我遇到了这个奇怪的错误:undefined method 'add_column' for #<Class:0x007f7a6a73cf30> (NoMethodError)。它似乎在以下迁移中抛出此错误:

class AddPresentationAttributesToPosts < ActiveRecord::Base
  add_column :presentations, :ticker, :string
  add_column :presentations, :action, :string
  add_column :presentations, :thesis, :string
  add_column :presentations, :slideshare_url, :string
end

但是,所有这些都可以在本地运行,heroku run rake db:migrate 也可以在不引发错误的情况下运行。知道这可能是什么吗?

【问题讨论】:

    标签: ruby-on-rails ruby heroku rails-activerecord heroku-postgres


    【解决方案1】:

    如果你想写一个迁移,你应该继承ActiveRecord::Migration。并且不要忘记updownchange 方法:

    class AddPresentationAttributesToPosts < ActiveRecord::Migration
      def change
        add_column :presentations, :ticker, :string
        add_column :presentations, :action, :string
        add_column :presentations, :thesis, :string
        add_column :presentations, :slideshare_url, :string
      end
    end
    

    【讨论】:

    • 哎呀,真尴尬。看起来我不小心生成了模型而不是迁移。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多