undefined_method 'tag_count' using acts_as_taggable_on_steroids

360 views Asked by At

I am using acts_as_taggable to create a tag cloud for my application. I have a model 'Question' which is using the acts_as_taggable plug-in. I basically want to filter the tags for the question model.

I also have a 'Subject' model. So the relation between subjects and questions is that a subject has many questions, and a question belongs to a subject.

So when i call @subject.questions.tag_counts, it works fine. But say I call @subject.free_questions.tag_counts, where free_questions is a method I've defined, it gives a me an "undefined_method tag_counts for #<Array>. I basically want to find all the tags for a subset of the questions.

Can anybody suggest a workaround?

1

There are 1 answers

1
samg On BEST ANSWER

It may help to implement free_questions as a named_scope so you can call association methods on it.

Something like:

class Question < ActiveRecord::Base
  named_scope :free, :conditions => {:free => true} # conditions that make a question 'free'
end

Then you can:

@subject.questions.free

and I suspect this may work as well. (don't have a lot of experience with acts_as_taggable)

@subject.questions.free.tag_counts

When you use a named_scope (instead of a model method you've defined) you get back a proxy object that looks and acts like an Array, but allows you to chain ActiveRecord association methods onto it. Any methods that work on @subject.questions you should be able to call on @subject.questions.free.