Model method missing

I extended one of my model classes with a method, like this:

class Poll < ActiveRecord::Base

has_many :poll_answers

def find_active_for
# […] method body
end

end

However, when I tried this in the controller class:

def show
@classroom = Classroom.find params[:id]
@lesson_groups = LessonGroup.find :all, :conditions => [
‘classroom_id=?’, @classroom.id ], :include => :lessons

@poll = Poll.find_active_for # my model method
end

…I got a method_missing:

NoMethodError (undefined method find_active_for' for Poll:Class): /vendor/rails/activerecord/lib/active_record/base.rb:1129:inmethod_missing’
/app/controllers/classroom_controller.rb:9:in `show’

How is that possible? The class (Poll) gets loaded - I’ve inserted debug
“logger.info” in poll.rb before and after class definition and they show
up in the logs.

And by the way, I tried accessing this method in a unit test as well,
with the same results.

Filip Koczorowski wrote:

end

And by the way, I tried accessing this method in a unit test as well,
with the same results.

Put a “self.” at the front of the method name:

def self.find_active_for

[…] method body

end


Michael W.

Michael W. wrote:

Put a “self.” at the front of the method name:

def self.find_active_for

[…] method body

end

Thanks, that’s it!

For some odd reason I forgot that the method I was trying to add was a
class method…