Hello,
I wrote a new class that I decided to put in the /app/lib folder. Part
way through one of the methods, I call #new on the model, update some of
the fields and then call #save (on the model).
When I do this though, I get a ‘const_missing’ error coming from Active
Support. Here’s the error:
“…/lib/active_support/dependencies.rb:105:in `const_missing’:
uninitialized constant ClassA::ModelA (NameError)”
The ‘ModelA’ is the model I’m trying to access, and ‘ClassA’ is the
class in /app/lib/class_a.rb. I’m not sure why it seems to think that
ModelA is a part of ClassA, perhaps that’s the problem?
I would have probably just used “require …/models/model_a.rb”, but I
found the following at StackOverflow:
“One way to solve this would be to explicitly require the model file
aircraft.rb. However, you will find this approach quickly leads to
insanity as it will break the Rails auto-loader in subtle and
astonishing ways. Rails is much easier if you work with the Rails class
loader, not against it.”
Source:
I then found an example at
http://www.strictlyuntyped.com/2008/06/rails-where-to-put-other-files.html
that suggests adding this to the file in the /app/lib folder:
def self.included(base)
base.extend(ClassMethods)
end
And then creating a file in /config/initializers with the following:
require ‘rails_extensions/user_logger’
ActionController::Base.class_eval do
include ActionController::UserLogger
end
Being new to Ruby/Rails, I’m really not sure what this supposed to do,
but I’m guessing it will work. That post was written in 2008 though, so
I’m thinking there may be a better way for this.
Does anyone have a suggestion as to the best way to get a class in the
/app/lib folder to see a model?
Thank you very much,
Mike