Model's method not being recognized when called

289 Views Asked by At

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.

2

There are 2 best solutions below

1
On BEST ANSWER

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


def get_parent
 ...
end
0
On

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.