views:

23

answers:

2

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.

+1  A: 

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


def get_parent
 ...
end
shingara
I tried this but it didn't work, and some friend told me to change it for def self.get_parent but it didn't work either.
Brian Roisentul
A: 

I solved it. The problem was that as I'm using acts_as_taggable_on plugin, the Tag model was already defined in its folder. So, I added the method in there and it worked.

Brian Roisentul