简体   繁体   English

NoMethodError(未定义方法),但是定义了类方法

[英]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: 这应该工作:

Project model 项目模型

  def self.get_all
    Project.all
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoMethodError:未定义的方法“ const_defined?” 当引用类方法时 - NoMethodError: undefined method `const_defined?' when referring to class methods NoMethodError:#的未定义方法“类型” <Class> - NoMethodError: undefined method `type' for #<Class> 来自类中Private方法的NoMethodError(未定义方法) - NoMethodError (undefined method) from Private method in class 返回NoMethodError的类方法:未定义的方法Rails - Class method returning NoMethodError: undefined method Rails mongoid NoMethodError(对于Complex:Class,未定义的方法“ all”) - mongoid NoMethodError (undefined method `all' for Complex:Class) NoMethodError未定义方法的怪异实例 - Weird instance of NoMethodError undefined method for class NoMethodError-InlineTextStorage:Class的未定义方法“更新” - NoMethodError - undefined method 'update' for InlineTextStorage:Class ImageUploader:Class的未定义方法“ include”(NoMethodError) - undefined method `Include' for ImageUploader:Class (NoMethodError) Rescue-NoMethodError — Brassring类的未定义方法日志 - Rescue - NoMethodError — undefined method log for brassring class ImageScience 的未定义方法“内联”:Class (NoMethodError) - undefined method 'inline' for ImageScience:Class (NoMethodError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM