[英]NoMethodError (undefined method) however class method is defined
I have created a new class Project which is inherited from ActiveRecord::Base . 我创建了一个新类Project ,该类继承自ActiveRecord :: Base 。 I defined a class method called get_all and I would like to use in Controller but I got NoMethodError (undefined method for ...)
我定义了一个名为get_all的类方法,我想在Controller中使用它,但是我遇到了NoMethodError(用于...的未定义方法)
Model: 模型:
class Project < ActiveRecord::Base
def self.get_all
find(:all)
end
end
Controller: 控制器:
class Controller < ApplicationController
unloadable
def index
@projects = Project.get_all
end
end
Note that in rails 3 the find(:all)
method ( without any options ) is deprecated in favor of the all
method. 请注意,在rails 3中,不推荐使用
find(:all)
方法(不带任何选项),而推荐使用all
方法。 More about it: 关于它的更多信息:
http://m.onkey.org/active-record-query-interface http://m.onkey.org/active-record-query-interface
Also, I don't know why are you making that function, when you could just do: 另外,我不知道您为什么要做那个功能,而您却只能这样做:
@projects = Project.all
just like chrisbulmer said. 就像克里斯布默所说。
This should work: 这应该工作:
def self.get_all
Project.all
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.