Rails:找不到nil错误类

时间:2015-07-21 06:25:49

标签: ruby-on-rails

我的代码找不到模块并返回nil错误。

undefined method `next' for nil:NilClass

这是代码

module Test
  class MyTestClass

    before_save :cid

    def cid
      MyTestClass.maximum(:id).next #error here, can't find MyTestClass 
    end
end

我尝试过像Test :: MyTestClass这样的变种但没有效果。

3 个答案:

答案 0 :(得分:1)

您不需要在方法' cid'中使用MyTestClass。

你可以使用:

module Test
  class MyTestClass

    before_save :cid

    def cid
      self.maximum(:id).next
    end
  end // End of class
end

答案 1 :(得分:0)

在这种情况下,您可以安全地使用maximum(:id).next

答案 2 :(得分:0)

MyTestClass.maximum(:id).next
-> self.maximum(:id).next
相关问题