Info from Ruby file to Model?

Hi,

Question...

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)

elsif self.question_type == "generator"

         require "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb"

         formatted = { :complete_question => :generated_question} end

I dont seem to be able to get the model to see the hash created in the "parks_bielkowski_test" file, and any suggestions would be helpful.

Thanks, DC

Can you make the file a module and mix it in?

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)

Fred

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"?

The test file contains a module

DC

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

Fred

Thanks Fred, I am reading about inheritance and modules. I think using the module is the correct way to do this but...

In model...

class Question < ActiveRecord::Base   require "#{RAILS_ROOT}/lib/generators/parks_bielkowski_test.rb"   include P_b

In parks_bielkowski_test.rb

module Parks_Bielkowski_test     def P_b         # Parks-Bielkowski step 1 result:         i = rand         if i < 0.5         ect...

I get the ERROR: uninitialized constant Question::P_b

Doesn't seem to be including P_b

Thanks, DC

Thanks Fred, I am reading about inheritance and modules. I think using the module is the correct way to do this but...

What you include is the module, not the methods in it

Fred

Frederick Cheung wrote in post #987535:

Thanks Fred, I am reading about inheritance and modules. I think using the module is the correct way to do this but...

What you include is the module, not the methods in it

Fred

Thanks Fred, Same error when I tried that:

uninitialized constant Question::Parks_Bielkowski_test

I'll do some more research. I really appreciate your help and guidance but obviously am missing something simple.

PS I am actually a non-programmer (physician) who is trying to write a single program-finding this is a steep learning curve!!

Thanks, DC

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.

Thank you.

DC

Frederick Cheung wrote in post #987535:

>> Thanks Fred, I am reading about inheritance and modules. I think using >> the module is the correct way to do this but...

> What you include is the module, not the methods in it

> Fred

Thanks Fred, Same error when I tried that:

uninitialized constant Question::Parks_Bielkowski_test

I'll do some more research. I really appreciate your help and guidance but obviously am missing something simple.

Have you still got the require there?

Fred