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

相關問題