ruby-on-railsacts-as-taggable

Model's method not being recognized when called


I'm using ruby on rails 2.3.2 and also using the acts_as_taggable_on puglin. That generated me two db tables: tags and taggings.

As I didn't need anything more from those, I didn't create a Tag model, for example. Now the project is more mature, I need to create some methods for tags, so I created a Tag model with some methods in it.

The model looks something like this:

class Tag < ActiveRecord::Base

  def self.get_parent
    parent = Tag.find(self.parent_id)

    return parent
  end
end

When I call it from a controller, it won't find the method. This is the code:

tag = Tag.find(tag_id)
the_parent = tag.get_parent

This will throw an error saying:

undefined method `get_parent' for #<Tag id: 13, name: "Historia", parent_id: 12>

I don't know what's wrong. Any help will be appreciated.


Solution

  • You define get_parent like a ClassMethod and you call it like instance method

    
    def get_parent
     ...
    end