我正在使用Elasticsearch-Model生成一个相当复杂的映射。我将所有与搜索相关的方法提取到一个ActiveSupport::Concern中。以下代码是在使用Tire gem时编写的,但我将gem升级到Elasticsearch-Rails和Elasticsearch-Model,因为Tire不支持Elasticsearch 1.x。当前的实现如下所示:
require 'active_support/concern'
module SearchBooks
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
mapping do
locales = %w['de', 'en', 'fr']
locales.each do |locale|
class_eval <<-RUBY
indexes: ...
RUBY
end
end
end但是我得到了这个错误:
/Users/xxx/.rvm/gems/ruby-2.0.0-p647/gems/activesupport-3.2.22/lib/active_support/core_ext/kernel/singleton_class.rb:11:in `class_eval': undefined method `indexes' for #<Class:0x007fea661037ec> (NoMethodError)我尝试了很多东西,例如使用def included(base),然后使用base.eval,但是我不能在正确的作用域下获得class_eval。
有什么想法吗?
发布于 2016-01-12 19:41:00
事实证明,解决方案非常简单:
只需使用instance_eval而不是class_eval,因为映射块的作用域是Elasticsearch::Model::Indexing::Mappings的一个实例。
https://stackoverflow.com/questions/34732331
复制相似问题