Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails索引任务_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails Rails索引任务

Ruby on rails Rails索引任务,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在尝试搜索以下索引任务 namespace :search_suggestions do desc "Generate search suggestions from items" task :index => :environment do SearchSuggestion.index_items end end def self.index_items Item.find_each do |item| index_term(item.title)

我正在尝试搜索以下索引任务

namespace :search_suggestions do
  desc "Generate search suggestions from items"
  task :index => :environment do
    SearchSuggestion.index_items
  end
end

def self.index_items
  Item.find_each do |item|
    index_term(item.title)
    item.title.split.each { |t| index_term(t) }
  end
end

def self.index_term(term)
  where(term: term.downcase).first_or_initialize.tap do |suggestion|
    suggestion.increment! :popularity
    suggestion.save!
  end
end
但是当I
rake search\u suggestions:index
时,列不会更新为当前值,旧的和已删除的数据仍然显示在表中

如果我这样做:

def self.index_items
 Search_suggestions.all
  Item.find_each do |item|
    index_term(item.title)
    item.title.split.each { |t| index_term(t) }
  end
end
然后所有内容都会正确更新,但这是正确的方法吗

更新1

删除项目的流程

 def items_destroy
    @item = Item.find(params[:id])
    @item.destroy
      respond_to do |format|
         format.html { redirect_to store_items_index_path(current_store), notice: 'Item was successfully destroyed.' }
         format.json { head :no_content }
    end
 end
route.rb

match "store/items/destroy"=> "stores#items_destroy", :via => :delete, :as => :store_items_destroy

lib/tasks/search\u建议。rake

namespace :search_suggestions do
  desc "Generate search suggestions from items"
  task :index => :environment do
    IndexItemsService.new.call
  end
end
app/services/index\u items\u service.rb

class IndexItemsService
  def call
    Item.find_each do |item|
      SearchSuggestion.index_terms([item.title].concat(item.title.split))
    end
    return true
  end
end
class SearchSuggestion < ActiveRecord::Base
  // other code
  def self.index_terms(terms)
    terms.each do |term|
      where(term: term.downcase).first_or_initialize.tap do |suggestion|
        suggestion.increment! :popularity
        suggestion.save!
      end
    end
  end
  // other code
end
def destroy
  @item = Item.find(params[:id])
  SearchSuggestion.where(term: [item.title].concat(item.title.split)).destroy_all
  @item.destroy
end
app/models/search\u suggestion.rb

class IndexItemsService
  def call
    Item.find_each do |item|
      SearchSuggestion.index_terms([item.title].concat(item.title.split))
    end
    return true
  end
end
class SearchSuggestion < ActiveRecord::Base
  // other code
  def self.index_terms(terms)
    terms.each do |term|
      where(term: term.downcase).first_or_initialize.tap do |suggestion|
        suggestion.increment! :popularity
        suggestion.save!
      end
    end
  end
  // other code
end
def destroy
  @item = Item.find(params[:id])
  SearchSuggestion.where(term: [item.title].concat(item.title.split)).destroy_all
  @item.destroy
end

危险!这主要是可行的,但它有明显的缺陷。让我们假设我们有两个标题为“Hello Anton”和“Hello Theopap”的项目。他们俩都被编入了索引。如果我们删除“Hello Anton”,它将销毁对“Hello Anton”的建议,这是正常的,“Anton”是正常的,但它也将销毁对“Hello”的建议,这两个项目之间是相交的。因此,第二项的“你好”建议也将被删除。在不改变架构的情况下,我看不到任何真正的解决方法来解决这个问题,等等

什么是
搜索建议
?是模型吗?是的,没错。。。SearchSuggestion是一个模型,它包含上述操作。除了位于
lib/tasks/search\u suggestions的rake任务操作之外。rake
为什么您的
SearchSuggestion
模型有一个方法,除了调用另一个模型
?。您的
搜索建议
是否与
项目
型号有关联?谢谢@AntonTkachov的回复。。。我正在尝试从这里实现自动完成:。。。我在我的搜索建议模型中没有关于项目模型的任何关联。我知道,Ryan为了教育目的试图使其更简单,但这样的代码太糟糕了:(。让我再看一次您的代码