3

I'm getting: undefined method 'important_method' for #<Class:0xbee7b80>

when I call: User.some_class_method

with:

# models/user.rb
class User < ActiveRecord::Base

  include ApplicationHelper

  def self.some_class_method
    important_method()
  end

end


# helpers/application_helper.rb
module ApplicationHelper

  def important_method()
    [...]
  end

end

What am I doing wrong? How can I avoid this problem?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
TomDogg
  • 3,803
  • 5
  • 33
  • 60

2 Answers2

1

include is generally used to include code at the instance level, where extend is for the class level. In this case, you'll want to look to have User extend ApplicationHelper. I haven't tested this, but it might be that simple.

RailsTips has a great write-up on include vs. extend - I highly recommend it.

CDub
  • 13,146
  • 4
  • 51
  • 68
  • Thanks for the hint. I also found another source for this issue at: http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/ and posted my final solution. – TomDogg Nov 13 '13 at 19:40
0

It's not DRY, but it works - change application_helper.rb to:

# helpers/application_helper.rb
module ApplicationHelper

  # define it and make it available as class method
  extend ActiveSupport::Concern
  module ClassMethods
    def important_method()
      [...]
    end
  end

  # define it and make it available as intended originally (i.e. in views)
  def important_method()
    [...]
  end

end
TomDogg
  • 3,803
  • 5
  • 33
  • 60