I am using the acts as taggable on steroids plugin and my web app has 4 models:
- Category
- Entry - acts as taggable and contains the following using single table inheritance
- Story
- Topic
Category has two important fields:
- tags:string - A list of tags associated with this category
- primary_tag:string - A single tag that gets assigned to any Topics created for the category
In the site have the following two pages:
- The Stories Page for a Category lists all stories tagged with that category's tags.
- The Topics Page for a Category lists all Topics AND Stories tagged with that category's tags.
The creation of Stories and Topics is as follows:
- When an Editor creates a Story they supply a list of tags to tag the story with.
- When a User creates a Topic it is created within the context of a Category. It is automagically tagged with the category's primary_tag upon creation.
I'm trying to define 3 has_many relationships for Category that uses tags to find associated Entries, Stories and Topics. If I were doing it in controller code I would use the following:
@category = Category.find(1)
@entries = Entry.find_tagged_with(@category.tags) # All Entries
@entries = Story.find_tagged_with(@category.tags) # Just Stories
@entries = Topic.find_tagged_with(@category.tags) # Just Topics
I'd like to make this an instance method of Category so that I call it just as follows:
@category.find(1)
@entries = @category.entries # All Entries
@entries = @category.stories # Just Stories
@entries = @category.topics # Just Topics
I can't figure out how/what to specify in the :has_many declaration to specify the Class methods above to do the work using the category instance in question.
I've tried this:
has_many :entries do
def tagged
Entry.find_tagged_with(self.tags)
end
end
But end up with the following error:
>> @category = Category.find(2)
=> #<Category id: 2, title: "Comics", description: "Comics", enabled: true, tags: "comics", created_at: "2009-06-22 13:29:52", updated_at: "2009-07-01 13:44:09", parent_id: nil, image: "", important: true, stories_enabled: true, topics_enabled: true, primary_tag: "comics">
>> @category.entries.tagged
NoMethodError: undefined method `tags' for #<Class:0x22781dc>
I've tried this.tags and reflection.tags but they all complaign about an undefined method, so there's something I definitely don't understand about the scope of the method.