需要使用红宝石的另一种方法调用self.method

app / models / product.rb

    class Product < ApplicationRecord
      def methode1.1
        # Do something
      end
      def method1
        # Do something
        methode1.1
      end
      def self.method2
        # Do something
        method1
      end
    end

在控制器中

def Method_4
  # Do something
  Product.method2
  # Do something
end

我从控制器呼叫method2。当我运行程序时。我遇到错误:

undefined local variable or method methode1 '' for class
LVCHA166 回答:需要使用红宝石的另一种方法调用self.method

您调用类方法Product.method2,并且它尝试调用实例方法method1。为此,您需要查找或初始化模型的实例,例如:

  # initialize
  def self.method2
    # Do something
    new.method1
  end

  # find
  def self.method2
    # Do something
    find_by(attr1: val1,attr2: val2).method1
  end
本文链接:https://www.f2er.com/3145505.html

大家都在问