我实现了elasticsearch,它似乎工作得很好,除非我想删除某些内容的记录。在我的控制器销毁方法中,我似乎无法在不执行rake environment tire:import CLASS=Stream FORCE=true
def destroy
#@stream = Stream.friendly.find(params[:id])
#Stream.tire.index(@stream).delete
#@stream.destroy
@stream.destroy
system "rake environment tire:import CLASS=Stream FORCE=true"
respond_to do |format|
format.html { redirect_to streams_url, notice: 'Stream was successfully destroyed.' }
format.json { head :no_content }
end
end
如果我使用注释掉的代码,我会收到错误:ActiveRecord::RecordNotFound (Couldn't find all Streams with 'id': (1, 2, 3, 4) (found 2 results, but was looking for 4)):
你应该如何与@stream一起正确删除索引?
编辑型号代码
class Stream < ActiveRecord::Base
extend FriendlyId
friendly_id :title
include Tire::Model::Search
include Tire::Model::Callbacks
def self.search(params)
tire.search(load: true) do
query { string params[:query], default_operator: "AND" } if params[:query].present?
end
end
end
答案 0 :(得分:0)
如果您在Tire::Model::Callbacks
模型中使用Stream
模块,则应自动将其删除。
如果你想从控制器手动运行它,代码可能会起作用。
@stream.destroy
@stream.tire.update_index