6

我有带有标签上下文的模型:

class Product < ActiveRecord::Base
  acts_as_taggable_on :categories
end

我正在尝试初始化标签缓存:

class AddCachedCategoryListToProducts < ActiveRecord::Migration
  def self.up
    add_column :products,  :cached_category_list, :string
    Product.reset_column_information
    products = Product.all
    products.each { |p| p.save_cached_tag_list }
  end
end

cached_category_list不初始化。我做错了什么?有没有人可以对这个 gem 使用缓存(我的版本是 2.0.6)?

4

2 回答 2

15

好吧,今天我遇到了同样的问题。我终于解决了,我的迁移缓存了所需的标签。您的迁移问题有两个方面:

  1. 设置缓存的 ActsAsTaggable 代码需要在列信息重置后再次运行。否则,不会创建缓存方法(请参阅https://github.com/mbleigh/acts-as-taggable-on/blob/v2.0.6/lib/acts_as_taggable_on/acts_as_taggable_on/cache.rb

  2. 您正在调用的方法save_cached_tag_list不会自动保存记录,因为它是作为before_save钩子安装的,并且它不想创建无限循环。所以你必须调用保存。

因此,尝试用以下内容替换您的迁移,它应该可以工作:

class AddCachedCategoryListToProducts < ActiveRecord::Migration
  def self.up
    add_column :products,  :cached_category_list, :string
    Product.reset_column_information
    # next line makes ActsAsTaggableOn see the new column and create cache methods
    ActsAsTaggableOn::Taggable::Cache.included(Product)
    Product.find_each(:batch_size => 1000) do |p|
      p.category_list # it seems you need to do this first to generate the list
      p.save! # you were missing the save line!
    end    
  end
end

那应该这样做。

于 2011-04-24T20:21:15.750 回答
-5

如果您将此与自有标签结合使用,则可能是问题所在。查看 gem 的代码,似乎不支持缓存拥有的标签

希望这可以帮助,

最佳,J

于 2011-02-20T20:22:40.727 回答