ruby Rails 模型的“未定义方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7901001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 04:25:10  来源:igfitidea点击:

"undefined method" for a rails model

rubyruby-on-rails-3

提问by Sebastien

I am using Devise with rails and i want to add a method "getAllComments", so i write this :

我正在使用带有 Rails 的设计,我想添加一个方法“getAllComments”,所以我写了这个:

    class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me, :city, :newsletter_register, :birthday, :postal_code,
          :address_complement, :address, :lastname, :firstname, :civility

      has_many :hotels_comments

      class << self # Class methods
          def getAllComments
             true
          end
      end
    end

And in my controller :

在我的控制器中:

def dashboard
  @user = current_user
  @comments = @user.getAllComments();
end

And when i go to my url, i got

当我去我的网址时,我得到了

 undefined method `getAllComments' for #<User:0x00000008759718>

What i am doing wrong?

我在做什么错?

Thank you

谢谢

回答by Douglas F Shearer

Because getAllCommentsis a class method and you are attempting to access it as an instance method.

因为getAllComments是一个类方法,而您正试图将它作为实例方法访问。

You either need to access it as:

您要么需要通过以下方式访问它:

User.getAllComments

or redefine it as an instance method:

或将其重新定义为实例方法:

class User < ActiveRecord::Base
  #...

  def getAllComments
    true
  end
end

def dashboard
  @user = current_user
  @comments = @user.getAllComments
end

回答by WarHog

As I can see, you make getAllComments as class method through addition it to eigenclass. And you try to call this method from instance.

正如我所看到的,您通过将 getAllComments 添加到 eigenclass 来将它作为类方法。你尝试从实例调用这个方法。

回答by Petr

Content of class << selfmeans class method. It could be shortened as def self.getAllComments

class << self手段类方法的内容。它可以缩短为def self.getAllComments

You should call it User.getAllCommentsand not @user.getAllComments

你应该调用它User.getAllComments而不是@user.getAllComments

回答by Fadhli Rahim

The getAllComments() method that you wrote is a class method

你写的 getAllComments() 方法是一个类方法

So the correct way to call the methods is

所以调用方法的正确方法是

@comments = User.getAllComments

But if you really want to scope the getAllComments to the current user, I recommend you write an instance method

但是如果你真的想把 getAllComments 的范围限定在当前用户,我建议你写一个实例方法

class User < ActiveRecord::Base
  ..
  def getAllComments
    // comments implementation
  end

So that way you can access the getAllComments method like so:

这样你就可以像这样访问 getAllComments 方法:

 @user = current_user
 @comments = @user.getAllComments