Forum: Ruby on Rails Undefined method error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
08af2dee79bb66d8d571e8706963cd00?d=identicon&s=25 Ilija Studen (Guest)
on 2006-01-23 10:30
Hi everybody,

Small problem. I get this error:

undefined method `all_categories' for BlogCategory:Class

Code of BlogCategory class is:

class BlogCategory < ActiveRecord::Base

  has_and_belongs_to_many :blog_posts

  validates_presence_of :name
  validates_uniqueness_of :name

  # Return all categories ordered by name
  def all_categories
    find :all, :order => 'name'
  end;

end

I call this function from FrontendController with simple:

@all_categories = BlogCategory.all_categories

As far as I can see there IS an all_categories method in BlogCategory
class, but from some reason I can't access it in controller.

Any ideas? I'm totally new to Ruby so I don't know where to start (tried
google, but with no luck).
C7669e8b5676f61fdf202230cbcf72d8?d=identicon&s=25 Mikkel Bruun (mikkel)
on 2006-01-23 10:39
Ilija Studen wrote:
> Hi everybody,
>
> Small problem. I get this error:
>
> undefined method `all_categories' for BlogCategory:Class
>
>   def all_categories
>     find :all, :order => 'name'
>   end;
>
>
> @all_categories = BlogCategory.all_categories
>

find is a class method.

do

 def all_categories
    BlogCategory.find :all, :order => 'name'
  end;
08af2dee79bb66d8d571e8706963cd00?d=identicon&s=25 Ilija Studen (Guest)
on 2006-01-23 10:55
Hi,

Updated the code with:

def all_categories
  BlogCategory.find(:all, :order => 'name')
end

I still get the same error...
5c19f2d52879a1e10670c7334ba4c7e3?d=identicon&s=25 Lugovoi Nikolai (Guest)
on 2006-01-23 11:01
(Received via mailing list)
def self.all_categories
   BlogCategory.find(:all, :order => 'name')
 end
C7669e8b5676f61fdf202230cbcf72d8?d=identicon&s=25 Mikkel Bruun (mikkel)
on 2006-01-23 11:31
Lugovoi Nikolai wrote:
> def self.all_categories
>    BlogCategory.find(:all, :order => 'name')
>  end

yes...
08af2dee79bb66d8d571e8706963cd00?d=identicon&s=25 Ilija Studen (Guest)
on 2006-01-23 13:01
Lugovoi Nikolai wrote:
> def self.all_categories
>    BlogCategory.find(:all, :order => 'name')
>  end

That's it. Thanks :)
This topic is locked and can not be replied to.