4

我有一个使用sunspot_solr的问题。我有一个应用程序使用这个宝石执行一些搜索。有5种型号具有此功能,但其中只有1示出了sunspot_solr - NoMethodError(undefined方法`结果='为零:NilClass)

@search.results 

NoMethodError (undefined method `result=' for nil:NilClass): 
    app/controllers/financial_dashboards_controller.rb:27:in `index' 

这里的错误是我的代码:

控制器

@search = Sunspot.search(Purchase) do 
    fulltext params[:search] 
    with(:product_owner).equal_to(current_user.id) 
    facet(:status) 
    if params[:status].present? 
     with(:status).equal_to(params[:status]) 
    end 
    facet(:sell_date) 
    if params[:sell_date].present? 
     with(:sell_date).equal_to(params[:sell_date]) 
    end 
    order_by(:sell_date, :desc) 
end 

#line 27 
@sales = @search.results.paginate(:page => params[:page], :per_page => 5) 

模型(采购):

searchable do 
    text :product_name 
    text :product_short_description 
    integer :product_owner 
    string :status 
    string :sell_date 
end   

def product_name 
    product.name 
end 

def product_short_description 
    product.short_description 
end 

def product_owner 
    product.user.id 
end 

def sell_date 
    date.to_s(:year_month) 
end 

#indicates status of the payment and delivery 
def status() 
    if !self.closed.nil? 
    I18n.t('purchases.status.finished') 
    elsif !self.measured.nil? 
    I18n.t('purchases.status.measured') 
    elsif !self.accomplished.nil? 
    I18n.t('purchases.status.delivered') 
    elsif !self.paid.nil? 
    I18n.t('purchases.status.paid') 
    elsif !self.canceled.nil? 
    I18n.t('purchases.status.canceled') 
    elsif !self.date.nil? 
    I18n.t('purchases.status.waiting_payment') 
    end 
end 

另一个奇怪的是,在我的开发机器上,这段代码完美地工作。在使用nginx的生产机器上,代码显示此错误。

我检查了宝石的版本,他们匹配。我试过

rake sunspot:solr:reindex RAILS_ENV=production 

重新索引。我试过

rake sunspot:solr:stop RAILS_ENV=production 
rake sunspot:solr:start RAILS_ENV=production 

重新启动搜索服务器。甚至试图删除solr /文件夹,并让启动脚本再次复制它。

为什么其他模型完美工作?任何想法如何解决这个问题?

谢谢

+2

你能找到你的问题的答案? –

+0

@ search.hits包含什么? –

回答

1

在我的情况下,这是一个关键字段(id)不唯一的情况。

发生这种情况是因为我设计了一个具有非独特id字段的mysql视图。

这就是为什么在下一个非唯一行索引过程中,太阳黑子总是首先击中无效的原因。

所以

hit.result = result 

太阳黑子宝石代码


某处引发的错误,当我想通了(我已经花了上几个小时),我只是做了我的id字段独特的,重塑我的模型和问题已经消失。

+0

是的,这也是我得到这个错误的原因,非唯一的ID字段。 – Darwayne

+0

我想投我的文章! )))今天我得到了同样的错误,去了所以找到了答案,只有我试图投票,我发现我写了它! ))) – okliv

相关问题