0

I have a model app/models/my_model.rb

class MyModel < ActiveRecord::Base
  ...
end

I have another class overridden from MyModel inside app/models/my_model_r_r.rb

class MyModelRR < MyModel
  ...
end

I have a helper file in app/helpers/v2/some_helper.rb where I want to use MyModelRR

module V2::SomeHelper
  # Want to use MyModelRR
end

I tried using:

# Inside some_helper.rb
require 'app/models/my_model_r_r.rb

# Throws error -- 
<home>/.rvm/gems/ruby-2.1.4/gems/activesupport-3.2.17/lib/active_support/dependencies.rb:251:in `require': cannot load such file -- app/models/my_model_r_r.rb (LoadError)

After this, I moved my_model_r_r.rb inside app/models/read_only, and added this in application.rb:

config.autoload_paths += Dir["#{config.root}/app/models/read_only"]

# Throws error:
NameError (uninitialized constant V2::SomeHelper::MyModelRR):

Any ideas how to solve this?

I'm using Rails 3.2 running on Ruby 1.9.3 (same error with Ruby 2.1)

EDIT: require 'app/models/my_model_r_r.rb' works perfectly fine when I switch to Ruby 1.8.7.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Rajat
  • 1,766
  • 2
  • 21
  • 42
  • You need to learn the difference between `require` and `require_relative` and what paths are searched by them both. See http://stackoverflow.com/q/3672586/128421 for more information. – the Tin Man Dec 15 '14 at 00:43

1 Answers1

0

This is because Ruby 1.9.2+ doesn't automatically add "." to the path, which was leading to this error. Changing this to

require './app/models/my_model_r_r.rb'

worked fine. Thanks to "the Tin Man" for this.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Rajat
  • 1,766
  • 2
  • 21
  • 42