Home>
I want a common method in

model and I don't want to instantiate it. I want to call it as a class method, but it becomes undefined method.
model/concerns/common_mehod.rb

module CommonMethod
  extend ActiveSupport :: Concern

    def self.set_factors_of (str, num)
      result = []
      unless str.blank?
        add_str = "_" + str.to_s
      else
        add_str = ""
      end
      num.times do | n |
        result<<"factor # {n + 1}" + add_str
      end
      return result
    end
end

model/item.rb
It is called in the instance method in this

include CommonMethod
def set_all_avgs
    @article || = self.article
    props = [: total_pt]
      props<<CommonMethod.set_factors_of ("avg", @ article.factors_num) ⬅️ This
    props.each do | prop |
      result = self.votes.average (prop)
      self.send (prop.to_s.gsub (/ pt /, "avg ="), result)
    end
  end

Console

irb (main): 002: 0>item.set_all_avgs
Traceback (most recent call last):
        2: from (irb): 2
        1: from app/models/item.rb: 25: in `set_all_avgs'
NoMethodError (undefined method `set_factors_of 'for CommonMethod: Module)

It must be defined as an instance method.

module CommonMethod
  extend ActiveSupport :: Concern

    def set_factors_of (str, num) ## Change
      result = []
      unless str.blank?
        add_str = "_" + str.to_s
      else
        add_str = ""
      end
      num.times do | n |
        result<<"factor # {n + 1}" + add_str
      end
      return result
    end
end
def set_all_avgs
    @article || = self.article
    props = [: total_pt]
      props<<self.set_factors_of ("avg", @ article.factors_num)
    props.each do | prop |
      result = self.votes.average (prop)
      self.send (prop.to_s.gsub (/ pt /, "avg ="), result)
    end
  end