Yes. In your module you’ve defined an instance method not a class
method.
But this:
class CarePhase < ActiveRecord::Base
include ModelHelper
pretend_helper("hi!")
Is the class trying to call the method, which fails because the class
doesn’t know about the ‘pretend_helper’ method.
What you have would work fine if you did this:
CarePhase.new.pretend_helper(‘hi’)
If you really do want a class method, try changing the method definition
in PretendHelper to:
def self.pretend_helper(msg)
puts msg
end
And then you would be fine to call:
CarePhase.pretend_helper(‘hi’)
Hope that helps.
– Josh
http://iammrjoshua.com
Roy P. wrote:
Ah yes–I see that thread now–thanks!
I’m on an airplane & can’t this minute check out the plugin Brandon
recommends, so I figured I’d try out the module approach you mention &
am having trouble.
I’ve got a file /lib/model_helper.rb w/the following contents:
module ModelHelper
def pretend_helper(msg)
puts(msg)
end
end
And a model in care_phase.rb that starts out like:
class CarePhase < ActiveRecord::Base
include ModelHelper
pretend_helper("hi!")
If I then open the console & type CarePhase.new I get this here:
Loading development environment (Rails 2.0.2)
CarePhase.new
NoMethodError: undefined method pretend_helper' for CarePhase(id: integer, name: string):Class from c:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1532:in
method_missing’
from C:/railsapps/collabtrac/app/models/care_phase.rb:9
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in
load_without_new_constant_marking' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:203:in
load_file’
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:342:in
new_constants_in' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:202:in
load_file’
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:94:in
require_or_load' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:248:in
load_missing_constant’
from
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:453:in
const_missing' from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:465:in
const_missing’
from (irb):1
Can anybody say what I’m doing wrong?
Thanks!
-Roy