2

私は、Tire を使用して Elastic Search に保持したい単純なモデルを備えた ActiveAdmin アプリを持っています。

class Person
  include Tire::Model::Persistence

  property :firstName
  property :lastName
end

しかし、インデックス アクションで次のエラーが発生します。

NoMethodError (undefined method `quoted_table_name' for Person:Class)

私は何を逃したのですか?

4

1 に答える 1

2

まず、タイヤの永続性を有効にするいくつかの手順をスキップしました。このトピックに関するChrisBerkhoutの投稿のおかげで、私のモデルは次のようになりました。

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パーシステンスで動作させることに関しては、ActiveModelまたはORMに依存しないバージョンが機能していますが、AAはActiveRecordに関連付けられていることがわかります。

于 2012-02-05T23:27:03.197 に答える