ruby-on-rails - 子类 ActsAsTaggableOn:Tag?
问题描述
创建时尝试对ActsAsTaggableOn:Tag
标签进行子类化。
module ActsAsTaggableOn
class Tag < ::ActiveRecord::Base
include AlgoliaSearch
TAG_INDEX = "Tags_#{Rails.env}"
algoliasearch per_environment: true do
attribute :name, :taggings_count
attributesToIndex ['name']
customRanking ['desc(taggings_count)']
end
end
end
我正在尝试做的事情: 创建标签后,在 Algolia 中对其进行索引。
发生了什么:
保存标签或使用的模型acts_as_taggable_on
(例如 Post)时,我收到此错误:
undefined method find_or_create_all_with_like_by_name
子类似乎ActsAsTaggableOn::Tag
无法找到父find_or_create_all_with_like_by_name
方法。
任何人都知道是否有办法继承 Tag 类?
解决方案
当您可以创建一个模块并使用它来用这个新功能装饰现有类时,我会质疑您是否真的想将它子类化。
module MyTagDecorator
def self.included(base)
base.class_eval do
include AlgoliaSearch
TAG_INDEX = "Tags_#{Rails.env}"
algoliasearch per_environment: true do
attribute :name, :taggings_count
attributesToIndex ['name']
customRanking ['desc(taggings_count)']
end
end
end
end
然后将模块包含在您要扩展的类中:
ActsAsTaggableOn::Tag.include(MyTagDecorator)
看:
推荐阅读
- jmeter - 使用 -J var=value 传递参数不适用于 Jmeter 远程测试执行
- javascript - 获取数据表中导出按钮的动态文件名和标题
- c++ - 用布尔类型包装返回值
- java - 通过单词比较字母
- uitableview - 如何根据 SwiftUI 中的内容大小设置 tableview 高度?
- python - Python 中的“@”运算符有什么作用?
- python-3.x - 使用 ffmpeg 连接图像和 ts 文件的 Python 代码
- java - 如何将 ByteBuffer 转换为位图图像?
- reactjs - rxjs中的嵌套和展平有什么区别?
- performance - 如何使用 pytorch 构建的模型调整时间序列数据的超参数