为什么我无法从控制器内部访问模型中定义的此方法?

use*_*569 1 ruby debugging ruby-on-rails ruby-on-rails-3

我的视频控制器中有这种方法:

def notifications
  erase_notification_count
end
Run Code Online (Sandbox Code Playgroud)

它调用erase_notification_count我的用户模型中定义的方法:

def erase_notification_count
  new_notification_count += (self.notifications.count * (-1))
end
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

NameError in VideosController#notifications

undefined local variable or method `erase_notification_count' for #<VideosController:0x102f0fed0>
Run Code Online (Sandbox Code Playgroud)

为什么我收到此错误?我如何解决它?

jes*_*iss 5

erase_notification_count在User模型上定义了,这意味着您必须在User对象上调用它.您正试图直接在VideosController对象上调用它.