Ошибка при использовании постоянства шин - PullRequest
2 голосов
/ 03 февраля 2012

У меня есть приложение ActiveAdmin с простой моделью, которую я хочу сохранить в Elastic Search with Tire:

class Person
  include Tire::Model::Persistence

  property :firstName
  property :lastName
end

Но я получаю эту ошибку при действии индекса:

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

Что я пропустил?

1 Ответ

2 голосов
/ 06 февраля 2012

Прежде всего, я пропустил несколько шагов, включающих постоянство шин.Благодаря сообщению Криса Берхоута на эту тему моя модель теперь выглядит следующим образом:

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

И мне нужно было добавить задачу es.rake в lib / tasks, чтобы настроить индекс:

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

И инициализатор es.rb в config / initializers для определения имени индекса:

ES_INDEX_NAME = "#{Rails.application.class.parent_name.downcase}_#{Rails.env}"

Работает угощение!

Что касается попытки получить ActiveAdminработая с постоянством ES, я вижу, что AA привязан к ActiveRecord, хотя ActiveModel или независимая от ORM версия находится в разработке.

...