現在の環境のインデックス名のプレフィックス
環境(この場合はテスト環境)ごとに異なるインデックス名を設定できます。
たとえば、でイニシャライザを作成できます
config/initializers/tire.rb
次の行で:
Tire::Model::Search.index_prefix "#{Rails.application.class.parent_name.downcase}_#{Rails.env.to_s.downcase}"
インデックスを削除するための考えられるアプローチ
Customer、Order、Productという名前のモデルがあると仮定して、次のコードをtest-startup / before-block/each-run-blockのどこかに配置します。
# iterate over the model types
# there are also ways to fetch all model classes of the rails app automaticly, e.g.:
# http://stackoverflow.com/questions/516579/is-there-a-way-to-get-a-collection-of-all-the-models-in-your-rails-app
[Customer, Order, Product].each do |klass|
# make sure that the current model is using tire
if klass.respond_to? :tire
# delete the index for the current model
klass.tire.index.delete
# the mapping definition must get executed again. for that, we reload the model class.
load File.expand_path("../../app/models/#{klass.name.downcase}.rb", __FILE__)
end
end
別
別の方法として、別のポート、たとえば1234でテストするために別のElasticSearchインスタンスを設定することもできます。その後、enviornment/test.rbで設定できます。
Tire::Configuration.url "http://localhost:1234"
そして、適切な場所(テストのスタートアップなど)で、ElasticSearchテストのすべてのインデックスを削除できます-インスタンス:
Tire::Configuration.client.delete(Tire::Configuration.url)
モデルクラスのTire-Mapping定義がまだ呼び出されていることを確認する必要があるかもしれません。