0

I am currently getting the following error on my post model which is under act_as_taggable_on for tags and channels.

undefined local variable or method `tag_list_on' for #
<ActiveRecord::Relation:0x007f9864675b48>

I seems that rails cannot detect the existence of tag_list_on or set_tag_list_on methods; however, it does detect the tagged_with method, whose source is located in the exact same module as the other files.

RubyMine can detect the existence of all of these methods fine btw.

Here is the section of code that I'm performing all of these operations on.

@posts = Post.tagged_with(params[:tags]).paginate(:page => params[:page]|| 1, :per_page => 20)
user_tags = @posts.tag_list_on(:tags)                                                         
custom_tags = user_tags - params[:tags]                                                       
@posts.set_tag_list_on(:customs, custom_tags)                                                 
@tags = @posts.tag_counts_on(:customs, :order => "count desc").limit(10)                      
@channels = @posts.tag_counts_on(:channels, :order => "count desc")                           
jab
  • 5,673
  • 9
  • 53
  • 84

1 Answers1

1

tagged_with is a class method of Post, added by the acts_as_taggable_on gem.

@posts in your code is an instance of ActiveRecord::Relation, not the Post class itself or any instance of it.

There is no tag_list_on instance method in ActiveRecord::Relation, hence the error.

tagged_with says it returns

...a scope of objects that are tagged with the specified tags.

tag_list_on and set_tag_list_on are instance methods of the Post class, added by the acts_as_taggable gem.

You need to call tag_list_on and set_tag_list_on for each instance of @posts

user_tags = []
@posts.each do |post|
  user_tags = user_tags + post.tag_list_on(:tags) 
end
custom_tags = user_tags.uniq - params[:tags]

# ...
deefour
  • 34,974
  • 7
  • 97
  • 90
  • Awesome, but tell me something though. Why do they only extend the use of some methods on ActiveRecord::Relation, but not all main methods that, one way or another, are probably going to be used pretty often together. – jab Aug 09 '12 at 19:43
  • Also, how would you go about setting the tag list that was generated correctly? If I call set_tag_list_on for each instance of post and then call @posts.tag_counts_on(:customs, :order => "count desc").limit(10); I don't think I get correct behavior. – jab Aug 09 '12 at 20:37