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")                           
4

1 に答える 1

1

tagged_withは、 gemPostによって追加されたのクラスメソッドです。acts_as_taggable_on

@postsコードには、クラス自体やそのインスタンスでActiveRecord::Relationはなく、のインスタンスが含まれています。Post

tag_list_onにインスタンスメソッドがないActiveRecord::Relationため、エラーが発生します。

tagged_withそれが戻ると言います

...指定されたタグでタグ付けされたオブジェクトのスコープ。

tag_list_onおよびset_tag_list_onは、 gemによって追加されたクラスのインスタンスメソッドです。Postacts_as_taggable

tag_list_onとを呼び出す必要がset_tag_list_onあります@posts

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

# ...
于 2012-08-09T16:54:51.180 に答える