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:in `method_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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Filip Koczorowski wrote:> 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:in > `method_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. >Put a "self." at the front of the method name: def self.find_active_for # [...] method body end -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Michael Wang 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... -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---