仅限 Heroku:祖先 gem 因未定义的方法“祖先”而崩溃 #<Model:0x#######>

Heroku only: ancestry gem crashes with undefined method `ancestry' for #<Model:0x#######>

我在 Heroku 上看到一个问题,只有祖先 gem(无法在本地重现): https://github.com/stefankroes/ancestry

home#upload 发生 NoMethodError: #

的未定义方法“祖先”

方法如下所示:

def anthem_upload
    user = User.find_by(id: params[:user_id])
    anthem = Anthem.create!(user_id: user.id, body:params[:post]) <-- HAPPENS HERE
   ...
ene

此方法甚至不调用 .ancestry 方法 - 但一旦创建模型就会发生异常。

模型看起来像这样:

class Anthem < ActiveRecord::Base
  has_ancestry
 ...
end

这是日志:

A NoMethodError occurred in home#anthem_upload:

  undefined method `ancestry' for #<Anthem:0x007f1684d58b98>
  app/controllers/home_controller.rb:335:in `anthem_upload'

您必须 运行 迁移才能将方法 ancestry 添加到您的模型:

 heroku run rake db:migrate

当然假设您的应用程序在开发阶段正常运行。如果没有,您可以使用

生成迁移
rails g migration add_ancestry_to_[table] ancestry:string

取自ancestry

已编辑

看了你们的评论,我确实尝试了我的假设。将 ancestry gem 添加到不需要的项目,然后将 has_ancestry 添加到模型 Task,无需迁移,在控制台我得到:

Loading development environment (Rails 4.2.2)
~/ (development) > Task.new
=> #<Task:0x00000008a3d088> {
               :id => nil,
       :project_id => nil,
             :name => nil,
      :description => nil,
    :points_budget => nil,
       :created_at => nil,
       :updated_at => nil
}
~/ (development) > Task.new.save
   (0.2ms)  BEGIN
   (0.2ms)  ROLLBACK
NoMethodError: undefined method `ancestry' for #<Task:0x00000008a9c998>
from /home/alejandro/.rvm/gems/ruby-2.2.2@rails4/gems/activemodel-4.2.2/lib/active_model/attribute_methods.rb:433:in `method_missing'
~/ (development) > Task.new.validate
NoMethodError: undefined method `ancestry' for #<Task:0x00000008ad1210>
from /home/alejandro/.rvm/gems/ruby-2.2.2@rails4/gems/activemodel-4.2.2/lib/active_model/attribute_methods.rb:433:in `method_missing'

你得到了同样的错误。当然,我正在开发中。你必须在 heroku 上尝试这样的事情:heroku run rails console