开发者

Model's method not being recognized when called

开发者 https://www.devze.com 2022-12-23 02:20 出处:网络
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.

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, i开发者_StackOverflowt 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.


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


def get_parent
 ...
end


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消