我有一个带有简单模型的 ActiveAdmin 应用程序,我想在使用轮胎的弹性搜索中坚持:
class Person
include Tire::Model::Persistence
property :firstName
property :lastName
end
但我在索引操作中收到此错误:
NoMethodError (undefined method `quoted_table_name' for Person:Class)
我错过了什么?
我有一个带有简单模型的 ActiveAdmin 应用程序,我想在使用轮胎的弹性搜索中坚持:
class Person
include Tire::Model::Persistence
property :firstName
property :lastName
end
但我在索引操作中收到此错误:
NoMethodError (undefined method `quoted_table_name' for Person:Class)
我错过了什么?
首先,我跳过了启用轮胎持久性的几个步骤。感谢Chris Berkhout 关于这个主题的帖子,我的模型现在看起来像这样:
class Person
include Tire::Model::Persistence
include Tire::Model::Search
include Tire::Model::Callbacks
index_name ES_INDEX_NAME
# Refresh ES so any changes are immediately visible
refresh = lambda { Tire::Index.new(ES_INDEX_NAME).refresh }
after_save &refresh
after_destroy &refresh
property :firstName
property :lastName
property :updated_at
before_save { |n| n.updated_at = Time.now }
def self.touch_es
# Ensure a mapping in a fresh index, so that Note.all can sort on updated_at
n = Person.new
n.save
n.destroy
end
def self.all
# Override so that Note.all comes back sorted on updated_at, rather than _id
search { sort { by :updated_at, 'desc' } }
end
def self.q(q)
search { sort { by :updated_at, 'desc' }; query { string q } }
end
end
我需要在 lib/tasks 中添加一个 es.rake 任务来设置索引:
namespace :es do
desc "Delete the ElasticSearch index for the current environment"
task :drop => :environment do
Tire::Index.new(ES_INDEX_NAME).delete
end
desc "Create the ElasticSearch index for the current environment"
task :create => :environment do
Tire::Index.new(ES_INDEX_NAME).create
Person.touch_es
end
end
以及 config/initializers 中的初始化程序 es.rb 来定义索引名称:
ES_INDEX_NAME = "#{Rails.application.class.parent_name.downcase}_#{Rails.env}"
工作一种享受!
至于尝试让 ActiveAdmin 与 ES 持久性一起工作,我看到 AA 与 ActiveRecord 相关联,尽管 ActiveModel 或独立于 ORM 的版本正在开发中。