Why can I not create an instance of a class I have created, in the app/decorator folder for my rails project, in the console?

Viewed 70

I have made a folder and file

decorators/my_model_decorator.rb 

and in it created the class

require 'delegate'

class MyModelDecorator < SimpleDelegator
  attr_reader :my_model

  def initialize(my_model)
    @my_model = my_model
  end

  def containable
    ContainableDecorator.new(my_model.containable)
  end
end

But referring to it in the like this

instance_x = MyModel.new

decorated_my_model = MyModelDecorator.new(instance_x)

Throws this error

NameError (uninitialized constant MyModelDecorator)

(Weirdly, I am able to run the code in the browser and the error message does know about the decorator class and I can access its methods in the console on the browser. )

Why am I getting this behaviour? Do I need to do something to “initialize” the class I have created?

according to this thought.co article

"You'll see this error when the code refers to a class or module that it can't find, often because the code doesn't include require, which instructs the Ruby file to load the class."

I don't quite understand this. Are you supposed to tell Rails to require MyModleDecorator somewhere? If so is there a particular place I am supposed to put this code?

0 Answers
Related