2014-10-09 180 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