0

lib/modules/file_type.rb

module Modules
  module Type
    def friend_name(type:)
      ...
    end
  end
end

app/models/car.rb

class Car < ApplicationRecord
  include Modules::Type

  def self.to_array
  ...
  name = friend_name(type: 'test')
  ...
  end
end

But I am getting this error:

undefined method `friend_name'

I am not sure why I am getting this error.

Anyone can help me?

Remy Wang
  • 666
  • 6
  • 26
  • 1
    `friend_name` is an instance method of the `Car` class, so `Car.new.friend_name(type: 'test')` works, but `Car.friend_name(type: 'test')` does not. – Marek Lipka Dec 11 '19 at 08:59
  • What is this code supposed to actually achieve? – max Dec 11 '19 at 11:27

1 Answers1

3

If friend_name is a class method then instead of include use extend in Car model

extend Modules::Type

More info about difference between include and extend could be found here -

What is the difference between include and extend in Ruby?

Hope that helps!

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78