I want a model to use info from a file (parks_bielkowski_test.rb)
residing in lib (standard ruby file containing a method called
"generated_question", that returns a hash).
The way I have it set up, I get "You have a nil object when you didn't
expect it!" if I use the symbol :generated_question in the model (see
below) or, "undefined local variable or method `question_instance' for
#<Question:0x00000100b35be0>" if I try it without the preceding colon
(generated_question)
What does parks_bielkowski_test.rb look like? ( require 'generators/
parks_bielkowski_test' should be enough - your app's lib folder
should be in ruby's load path)
I will have hundreds of methods I need the model to call
on depending on the situation (only one at a time), where should they
be kept, and how to call them?
I have tried...
require "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb" in the
model to access the test file but it does not seem to return a value.
Is this the correct use of "require"?
I will have hundreds of methods I need the model to call
on depending on the situation (only one at a time), where should they
be kept, and how to call them?
I have tried...
require "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb" in the
model to access the test file but it does not seem to return a value.
Is this the correct use of "require"?
Require just loads the file. You don't get a meaningful return value from it
The test file contains a module
If you've defined your method in a module then in order to call that method you need to include it in the current object (alternatively you could make it a module method and then call SomeModule.method_name)
You might do well to do a little reading on how method lookup, modules, inheritance etc work in ruby
The book Metaprogramming in Ruby teaches everything you need to know
about modules and more. Also, you might the book Eloquent Ruby a very
nice introduction to Ruby and its finer points. It's well-written and
dives into all facets (no pun intended) of Ruby.