2014-10-09 30 views
0

我想讓我的搜索方法爲Heoku工作。我之前遇到過這個問題,並將.downcase添加到它中。 但現在,它不工作,我得到 未定義的方法`downcase」的零:NilClass未定義的方法downcase

listing.rb

def self.locsearch(search_location, search_description) 
    return scroped unless search_location.present? && search_description.present? 
    where(["LOWER(location) LIKE? AND LOWER(description) LIKE?", "%#{search_location.downcase}%", "%#{search_description.downcase}%"]) 
end 

有誰知道問題是什麼?

我改成了

def self.locsearch(location, description) 
    if location.present? && description.present? 
    where(["LOWER(location) LIKE? AND LOWER(description) LIKE?", "%#{location.downcase}%", "%#{description.downcase}%"]) 
else 
    self.all 
end 
end 

現在它返回的所有目錄,即使我的位置和說明,輸入相匹配的某些上市。 爲什麼?

回答

1

只有當search_location和search_description都存在時,您纔會返回scroped。

如果其中只有一個爲零,則執行下一行,並在nil上調用downcase。

我會重寫你的代碼是這樣的:

self.search_location(location, description) 
    if location.present? && description.present? 
     where(["LOWER(location) LIKE? AND LOWER(description) LIKE?", 
      "%#{location.downcase}%", "%#{description.downcase}%"]) 
    else 
     scroped #not sure what it is, maybe you need another name for this? 
    end 
end 

這樣更容易發現錯誤。

+0

謝謝馬克,這個作品。我用self.all代替了scroped(意思是有範圍的,但我用它錯了)。 – Rika 2014-10-09 20:12:25

1

search_location或其他search_description的值是nil,因此當您嘗試調用其downcase方法時,沒有值。因此錯誤。

順便說一句,你可能是指scoped,而不是scroped

+0

是的我打算寫範圍感謝 – Rika 2014-10-09 19:38:35

相關問題