只有Heroku:祖先的宝石崩溃与未定义的方法`祖先&#39;对于#<model:0x #######>

时间:2015-10-02 02:46:18

标签: ruby-on-rails ancestry

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

home#upload发生NoMethodError:  未定义的方法`祖先&#39;对于#

方法如下:

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'

1 个答案:

答案 0 :(得分:2)

您必须运行迁移才能将方法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